Skip to content

BUG: np.nan to datetime assertionerror when too large datetime given #61671

Open
@dnallicus

Description

@dnallicus

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
import numpy as np
import datetime
pd.DataFrame([np.nan], dtype='datetime64[ns]').replace(np.nan, datetime.datetime(3000,1,1))

Issue Description

code above gives this error, likely because the datetime is too big for datetime64[ns]:

pd.DataFrame([np.nan], dtype='datetime64[ns]').replace(np.nan, datetime.datetime(3000,1,1))

File "C:\Users\mdarnall\mdarnall-local-dev\tma-venv-prod.venv\lib\site-packages\pandas\core\generic.py", line 8141, in replace
new_data = self._mgr.replace(
File "C:\Users\mdarnall\mdarnall-local-dev\tma-venv-prod.venv\lib\site-packages\pandas\core\internals\base.py", line 249, in replace
return self.apply_with_block(
File "C:\Users\mdarnall\mdarnall-local-dev\tma-venv-prod.venv\lib\site-packages\pandas\core\internals\managers.py", line 363, in apply
applied = getattr(b, f)(**kwargs)
File "C:\Users\mdarnall\mdarnall-local-dev\tma-venv-prod.venv\lib\site-packages\pandas\core\internals\blocks.py", line 924, in replace
blk = self.coerce_to_target_dtype(value)
File "C:\Users\mdarnall\mdarnall-local-dev\tma-venv-prod.venv\lib\site-packages\pandas\core\internals\blocks.py", line 490, in coerce_to_target_dtype
raise AssertionError(
AssertionError: Something has gone wrong, please report a bug at https://github.com/pandas-dev/pandas/issues

Expected Behavior

different error message or change column type

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.9.13
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 151 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 2.2.3
numpy : 2.0.2
pytz : 2025.2
dateutil : 2.9.0.post0
pip : None
Cython : None
sphinx : None
IPython : 8.18.1
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
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.4.0
matplotlib : 3.9.4
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 20.0.0
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.1
sqlalchemy : 2.0.41
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

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