Skip to content

Commit cf1cd0d

Browse files
committed
Fix pandas-dev#61208: OverflowError when fillna on DataFrame with a pd.Timestamp
- Now correctly raises OutOfBoundsDatetime - Added test_fillna_out_of_bounds_datetime()
1 parent 56847c5 commit cf1cd0d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pandas/core/internals/blocks.py

+2
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,8 @@ def putmask(self, mask, new) -> list[Block]:
17491749
try:
17501750
# Caller is responsible for ensuring matching lengths
17511751
values._putmask(mask, new)
1752+
except OutOfBoundsDatetime as e:
1753+
raise
17521754
except (TypeError, ValueError):
17531755
if self.ndim == 1 or self.shape[0] == 1:
17541756
if isinstance(self.dtype, IntervalDtype):

pandas/tests/frame/methods/test_fillna.py

+17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
)
1616
import pandas._testing as tm
1717
from pandas.tests.frame.common import _check_mixed_float
18+
from pandas.errors import OutOfBoundsDatetime
1819

1920

2021
class TestFillNA:
@@ -781,3 +782,19 @@ def test_fillna_with_none_object(test_frame, dtype):
781782
if test_frame:
782783
expected = expected.to_frame()
783784
tm.assert_equal(result, expected)
785+
786+
787+
def test_fillna_out_of_bounds_datetime():
788+
# GH#61208
789+
df = DataFrame({
790+
'datetime': date_range('1/1/2011', periods=3, freq='h'),
791+
'value': [1, 2, 3]
792+
})
793+
df.iloc[0, 0] = None
794+
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)

0 commit comments

Comments
 (0)