Skip to content
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

BUG: concat with datetime index returns Series instead of scalar if microsecond=0 #57835

Open
3 tasks done
davetapley opened this issue Mar 14, 2024 · 2 comments
Open
3 tasks done
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@davetapley
Copy link
Contributor

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

from datetime import UTC, datetime

from pandas import DataFrame, concat

t1 = datetime.now(tz=UTC)
t2 = datetime.now(tz=UTC).replace(microsecond=0)

t1_str = str(t1)
t2_str = str(t2)

df1 = DataFrame({'a': [1]}, index=[t1])
print(type(df1.loc[t1].a))
print(type(df1.loc[t1_str].a))

df2 = DataFrame({'a': [2]}, index=[t2])
print(type(df2.loc[t2].a))
print(type(df2.loc[t2_str].a))

df = concat([df1, df2])

print(type(df.loc[t1].a))
print(type(df.loc[t1_str].a))

print(type(df.loc[t2].a))
print(type(df.loc[t2_str].a))

Issue Description

.a is correctly returned as numpy.int64 in all cases, except for the last line when I use t2_str and suddenly it's a one element Series containing the numpy.int64.

I have no idea what on earth is going on, it took a lot of 🔍 to get a repro.
I found it while writing a unit test where I was passing a timestamp from test data as a string.

If you remove the .replace(microsecond=0) you'll see it works as expected 🤯

Expected Behavior

.loc should be consistent before and after a concat.

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.11.6.final.0
python-bits : 64
OS : Linux
OS-release : 6.2.0-1019-azure
Version : #19~22.04.1-Ubuntu SMP Wed Jan 10 22:57:03 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.1
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.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 : 3.1.2
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : 2023.2.0
fsspec : 2023.10.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 14.0.1
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@davetapley davetapley added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 14, 2024
@dontgoto
Copy link
Contributor

Maybe, @jbrockmendel knows more, he last changed the relevant code in datetime._parse_with_reso.

There is some additional logic for handling the lookup with str labels on a DateTimeIndex that infers a lookup-resolution from the string itself. In the end, the str(t2) has no zero'd microseconds in it, thus a sec resolution is inferred, matching both t1and t2.

If one wants to keep this feature of resolution dependent lookup, one would have to add the trailing zeros to times like str(t2) manually, since datetime always removes them from what I could see.

@jbrockmendel
Copy link
Member

Correct, this is a feature called “partial string slicing” on dateteindex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants