Skip to content

BUG: DataFrame column assignment with pd.Timestamp leads to unexpected dtype and incorrect JSON output #61444

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
tanjt107 opened this issue May 15, 2025 · 4 comments · May be fixed by #61450
Closed
3 tasks done
Assignees
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timestamp pd.Timestamp and associated methods

Comments

@tanjt107
Copy link

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

date = pd.Timestamp("2025-01-01")
df = pd.DataFrame(columns=["date"], index=["a", "b", "c"])
df["date"] = date
print(df["date"].dtype)  # Output: datetime64[s] Expected: datetime64[ns]
print(df.to_json())  # Output: {"date":{"a":1696,"b":1696,"c":1696}}
# Expected: {"date":{"a":1735689600000,"b":1735689600000,"c":1735689600000}}

Issue Description

When assigning a pd.Timestamp to a column in a DataFrame, the resulting dtype of the column is not as expected, and the output of to_json() is incorrect.

Expected Behavior

The dtype of the date column should default to datetime64[ns] after assignment.
The output of df.to_json() should correctly represent the timestamp in milliseconds since the epoch.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.4
python-bits : 64
OS : Darwin
OS-release : 24.4.0
Version : Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.2.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None
None

@tanjt107 tanjt107 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 15, 2025
Farsidetfs added a commit to Farsidetfs/pandas that referenced this issue May 17, 2025
@Farsidetfs
Copy link

take

@Farsidetfs
Copy link

I was able to duplicate and believe my PR will resolve this issue. If confirmed as a bug worth fixing, will update tests and change document and submit PR for approval.

@rhshadrach
Copy link
Member

Thanks for the report, it seems to me the issue lies with Timestamp creation rather than assignment to a DataFrame.

date = pd.Timestamp("2025-01-01")
print(date.unit)
# s
date2 = pd.Timestamp(year=2025, month=1, day=1)
print(date2.unit)
# us

As such, closing as a duplicate of #58989

@rhshadrach rhshadrach added Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timestamp pd.Timestamp and associated methods and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 17, 2025
@Farsidetfs
Copy link

@rhshadrach

I think this is a separate issue as this version fails as well.

#this test fails
date2 = Timestamp(year=2025, month=1, day=1)
df4 = DataFrame(index=['a', 'b', 'c'], columns=["date"], dtype='datetime64[ns]')
df4["date"] = date2
assert df4["date"].dtype == "datetime64[ns]"
print("df4 assertion passed")

#this test passes
date = Timestamp("2025-01-01")
df2 = DataFrame(index=['a', 'b', 'c'], columns=["date"], dtype='datetime64[ns]')
df2["date"] = [date]*len(df2)
assert df2["date"].dtype == "datetime64[ns]"
print("df2 assertion passed")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Non-Nano datetime64/timedelta64 with non-nanosecond resolution Timestamp pd.Timestamp and associated methods
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants