Skip to content

BUG: Unexpected datetime dtype change on assignments #60978

@szev77

Description

@szev77

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

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 : 0691c5c
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions