Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [x] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

index = pd.DatetimeIndex(['2022-01-01 00:00:00', 'NaT',
               '2022-01-01 00:00:01', '2022-01-01 00:00:02',
               '2022-01-01 00:00:03', '2022-01-01 01:00:01',
               '2022-01-01 02:00:02', '2022-01-01 03:00:03',
               '2032-01-01 01:00:01', '2042-01-01 01:00:01',
               '2032-01-01 01:00:01', '2052-01-01 01:00:01',
               '2100-12-31 11:59:59'],
              dtype='datetime64[ns]', name='TIME', freq=None)
df = pd.DataFrame(index=index, data=list(range(13)))

# this correctly prints just the first row
print(df.first('1s'))
# this incorrectly also prints just the first row
print(df.first('2s'))
# this correctly prints 4 rows from the first 3 seconds, including 0
print(df.first('3s'))

Issue Description

first('1s') correctly prints just the first row, but first('2s') also only prints the first row.

Expected Behavior

first('2s') should gives rows 1 and 3 (indexing from 1) and exclude the null.

Installed Versions

/Users/maheshvashishtha/opt/anaconda3/envs/pandas-dev-alt/lib/python3.8/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils. warnings.warn("Setuptools is replacing distutils.") INSTALLED VERSIONS ------------------ commit : 2e218d10984e9919f0296931d92ea851c6a6faf5 python : 3.8.16.final.0 python-bits : 64 OS : Darwin OS-release : 21.5.0 Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 1.5.3 numpy : 1.24.1 pytz : 2022.7.1 dateutil : 2.8.2 setuptools : 65.6.3 pip : 22.3.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : 8.8.0 pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None tzdata : None

Comment From: topper-123

Thanks for the bug rapport.

This looks ok to me in the development branch:

>>> import pandas as pd
>>>
>>> index = pd.DatetimeIndex(['2022-01-01 00:00:00', 'NaT',
                   '2022-01-01 00:00:01', '2022-01-01 00:00:02',
                   '2022-01-01 00:00:03', '2022-01-01 01:00:01',
                   '2022-01-01 02:00:02', '2022-01-01 03:00:03',
                   '2032-01-01 01:00:01', '2042-01-01 01:00:01',
                   '2032-01-01 01:00:01', '2052-01-01 01:00:01',
                   '2100-12-31 11:59:59'],
                  dtype='datetime64[ns]', name='TIME', freq=None)
>>> df = pd.DataFrame(index=index, data=list(range(13)))
>>> df.first('1s')
            0
TIME
2022-01-01  0
>>> df.first('2s')
                     0
TIME
2022-01-01 00:00:00  0
2022-01-01 00:00:01  2
>>> df.first('3s')
                     0
TIME
2022-01-01 00:00:00  0
2022-01-01 00:00:01  2
2022-01-01 00:00:02  3

Do you have the development branch installed? If yes, can corroborate that this works as intended?

I'll close this issue, but if you see something as not working, I'll reopen of course.

Comment From: mvashishtha

@topper-123 I think we can close all the first and last issues because these methods are being deprecated: https://github.com/pandas-dev/pandas/issues/50884#issuecomment-1527415805