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
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