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.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
ts = pd.Timestamp.today()
df = pd.DataFrame({"A": [ts]})
print(df.dtypes)
# B now has datetime64[us] dtype
df["B"] = ts
print(df.dtypes)
# A remains datetime64[ns] dtype
df.loc[:, "A"] = pd.Timestamp.today()
print(df.dtypes)
# A now changes to datetime64[us] dtype
df["A"] = ts
print(df.dtypes)
# B remains datetime64[us] dtype
df.loc[:, "B"] = ts
print(df.dtypes)
Issue Description
Dataframe (and series) creation with a pd.Timestamp results in a dtype of datetime64[ns]
.
However when I assign the same timestamp to a new column the dtype changes to datetime64[us]
.
Assigning a different way maintains the original dtype.
Expected Behavior
I would expect both assignments to maintain the same dtype of datetime64[ns]
.
Installed Versions
INSTALLED VERSIONS
commit : 0691c5cf90477d3503834d983f69350f250a6ff7 python : 3.11.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19045 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United Kingdom.1252 pandas : 2.2.3 numpy : 1.26.0 pytz : 2023.3.post1 dateutil : 2.8.2 pip : 22.3 Cython : None sphinx : None IPython : 8.16.1 adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : 4.12.2 blosc : None bottleneck : 1.3.7 dataframe-api-compat : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : 3.1.4 lxml.etree : None matplotlib : 3.8.0 numba : 0.60.0 numexpr : 2.8.7 odfpy : None openpyxl : 3.1.4 pandas_gbq : None psycopg2 : 2.9.9 pymysql : None pyarrow : 11.0.0 pyreadstat : None pytest : 7.4.3 python-calamine : None pyxlsb : None s3fs : None scipy : 1.11.3 sqlalchemy : 2.0.0 tables : None tabulate : 0.9.0 xarray : None xlrd : 2.0.1 xlsxwriter : None zstandard : None tzdata : 2023.3 qtpy : None pyqt5 : None
Comment From: mroeschke
Thanks for the report.
On main (reflecting the future pandas 3.0), I get the correct behavior where all these assignments return us
resolution, the original resolution of ts
, so closing
In [5]: ts = pd.Timestamp.today()
...: df = pd.DataFrame({"A": [ts]})
In [6]: df["B"] = ts
...: print(df.dtypes)
A datetime64[us]
B datetime64[us]
dtype: object
In [7]: df.loc[:, "A"] = pd.Timestamp.today()
...: print(df.dtypes)
A datetime64[us]
B datetime64[us]
dtype: object
In [8]: df["A"] = ts
...: print(df.dtypes)
A datetime64[us]
B datetime64[us]
dtype: object
In [9]: df.loc[:, "B"] = ts
...: print(df.dtypes)
A datetime64[us]
B datetime64[us]
dtype: object
In [10]: ts.unit
Out[10]: 'us'