Skip to content

BUG: first(’2s’) with index with null in middle gives 1st row instead of first 2 #50882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
mvashishtha opened this issue Jan 20, 2023 · 2 comments
Closed
3 tasks done
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@mvashishtha
Copy link

mvashishtha commented Jan 20, 2023

Pandas version checks

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

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

  • 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 : 2e218d1
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

@mvashishtha mvashishtha added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 20, 2023
@topper-123
Copy link
Contributor

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.

@topper-123 topper-123 added Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 6, 2023
@mvashishtha
Copy link
Author

@topper-123 I think we can close all the first and last issues because these methods are being deprecated: #50884 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants