Skip to content

Commit 1490ca2

Browse files
committed
BUG: OverflowError when fillna on DataFrame with a pd.Timestamp (pandas-dev#61208)
1 parent de8f41b commit 1490ca2

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ Datetimelike
660660
- Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
661661
- Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)
662662
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)
663+
- Bug in :meth:`DataFrame.fillna` raising an `AssertionError` instead of `OutOfBoundsDatetime` when filling a `datetime64[ns]` column with an out-of-bounds timestamp (e.g., `'0001-01-01'`). Now correctly raises `OutOfBoundsDatetime`. (:issue:`61208`)
663664

664665
Timedelta
665666
^^^^^^^^^

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ def putmask(self, mask, new) -> list[Block]:
17461746
try:
17471747
# Caller is responsible for ensuring matching lengths
17481748
values._putmask(mask, new)
1749-
except OutOfBoundsDatetime as e:
1749+
except OutOfBoundsDatetime:
17501750
raise
17511751
except (TypeError, ValueError):
17521752
if self.ndim == 1 or self.shape[0] == 1:

pandas/tests/frame/methods/test_fillna.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.errors import OutOfBoundsDatetime
5+
46
from pandas import (
57
Categorical,
68
DataFrame,
@@ -15,7 +17,6 @@
1517
)
1618
import pandas._testing as tm
1719
from pandas.tests.frame.common import _check_mixed_float
18-
from pandas.errors import OutOfBoundsDatetime
1920

2021

2122
class TestFillNA:
@@ -782,19 +783,15 @@ def test_fillna_with_none_object(test_frame, dtype):
782783
if test_frame:
783784
expected = expected.to_frame()
784785
tm.assert_equal(result, expected)
785-
786-
786+
787+
787788
def test_fillna_out_of_bounds_datetime():
788789
# GH#61208
789-
df = DataFrame({
790-
'datetime': date_range('1/1/2011', periods=3, freq='h'),
791-
'value': [1, 2, 3]
792-
})
790+
df = DataFrame(
791+
{"datetime": date_range("1/1/2011", periods=3, freq="h"), "value": [1, 2, 3]}
792+
)
793793
df.iloc[0, 0] = None
794794

795-
msg="Cannot cast 0001-01-01 00:00:00 to unit='ns' without overflow"
796-
with pytest.raises(
797-
OutOfBoundsDatetime,
798-
match=msg
799-
):
800-
df.fillna(Timestamp('0001-01-01'), inplace=True)
795+
msg = "Cannot cast 0001-01-01 00:00:00 to unit='ns' without overflow"
796+
with pytest.raises(OutOfBoundsDatetime, match=msg):
797+
df.fillna(Timestamp("0001-01-01"), inplace=True)

0 commit comments

Comments
 (0)