Description
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