-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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 datetime
import pandas as pd
df = pd.DataFrame({'str': ['2020-01-01 00:00:00.123456789+00:00']})
df['ts'] = pd.to_datetime(df['str'])
df['ts_nano'] = df['ts'].astype('datetime64[ns, UTC]')
df['ts_micro'] = df['ts'].astype('datetime64[us, UTC]')
df['ts_milli'] = df['ts'].astype('datetime64[ms, UTC]')
df['ts_s'] = df['ts'].astype('datetime64[s, UTC]')
print(df.dtypes)
print()
for col in df.columns:
print(f"{col.ljust(10)} {df[col].iloc[0]}")
print()
print("CSV")
csv_str = df.T.to_csv(index=False)
print(csv_str)Issue Description
In repro we see:
str object
ts datetime64[ns, UTC]
ts_nano datetime64[ns, UTC]
ts_micro datetime64[us, UTC]
ts_milli datetime64[ms, UTC]
ts_s datetime64[s, UTC]
dtype: object
str 2020-01-01 00:00:00.123456789+00:00
ts 2020-01-01 00:00:00.123456789+00:00
ts_nano 2020-01-01 00:00:00.123456789+00:00
ts_micro 2020-01-01 00:00:00.123456+00:00
ts_milli 2020-01-01 00:00:00.123000+00:00
ts_s 2020-01-01 00:00:00+00:00
Observations:
ts_micro prints as a truncated string, i.e. 789 are omitted, but:
ts_milli replaces the 456 with 000.
This behavior is repeated in to_csv output, which is where I encountered the inconsistency.
Expected Behavior
Either truncate the output or replace with zeros, not both.
Installed Versions
Details
INSTALLED VERSIONS
commit : 0691c5c
python : 3.12.2
python-bits : 64
OS : Linux
OS-release : 6.8.0-1030-azure
Version : #35~22.04.1-Ubuntu SMP Mon May 26 18:08:30 UTC 2025
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.3
numpy : 1.26.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : None
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 : 5.2.2
matplotlib : None
numba : 0.62.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 17.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None