Skip to content

BUG: to_datetime does not recast units when passing a DatetimeIndex #64012

@jf2

Description

@jf2

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
dt_range = pd.date_range(start="2026-01-01", periods=10, freq="D", unit="us")
assert pd.to_datetime(dt_range, unit="ns").dtype == "datetime64[ns]"

Issue Description

to_datetime appears to be returning the passed DatetimeIndex without checking for a different unit requested.

Expected Behavior

In case of unit change, I would expect the underlying dtype to be recast (and in case values not representable in the new unit, an exception to be thrown)

Installed Versions

Details

INSTALLED VERSIONS

commit : 366ccdf
python : 3.11.4
python-bits : 64
OS : Linux
OS-release : 4.18.0-513.5.1.el8_9.x86_64
Version : #1 SMP Mon Nov 20 08:56:15 EST 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 3.0.0
numpy : 2.4.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : None
sphinx : None
IPython : 9.5.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.14.3
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : 6.0.2
matplotlib : 3.10.6
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
psycopg2 : None
pymysql : None
pyarrow : 23.0.0
pyiceberg : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : 2025.2
pyxlsb : None
s3fs : None
scipy : 1.17.0
sqlalchemy : 2.0.46
tables : None
tabulate : None
xarray : None
xlrd : 2.0.2
xlsxwriter : None
zstandard : None
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