Skip to content

Commit 80795df

Browse files
authored
BUG: OverflowError when fillna on DataFrame with a pd.Timestamp (#61208) (#61216)
* Fix #61208: OverflowError when fillna on DataFrame with a pd.Timestamp - Now correctly raises OutOfBoundsDatetime - Added test_fillna_out_of_bounds_datetime() * Comply with pre-commit and added an entry in v3.0.0.rst * Removed flag 'inplace=True' from test and fixed the bug for this case.
1 parent ca20ea9 commit 80795df

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Diff for: doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ Datetimelike
651651
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56147`)
652652
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
653653
- Bug in :func:`tseries.frequencies.to_offset` would fail to parse frequency strings starting with "LWOM" (:issue:`59218`)
654+
- Bug in :meth:`DataFrame.fillna` raising an ``AssertionError`` instead of ``OutOfBoundsDatetime`` when filling a ``datetime64[ns]`` column with an out-of-bounds timestamp. Now correctly raises ``OutOfBoundsDatetime``. (:issue:`61208`)
654655
- Bug in :meth:`DataFrame.min` and :meth:`DataFrame.max` casting ``datetime64`` and ``timedelta64`` columns to ``float64`` and losing precision (:issue:`60850`)
655656
- Bug in :meth:`Dataframe.agg` with df with missing values resulting in IndexError (:issue:`58810`)
656657
- Bug in :meth:`DatetimeIndex.is_year_start` and :meth:`DatetimeIndex.is_quarter_start` does not raise on Custom business days frequencies bigger then "1C" (:issue:`58664`)

Diff for: pandas/core/internals/blocks.py

+4
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,8 @@ def where(self, other, cond) -> list[Block]:
16791679

16801680
try:
16811681
res_values = arr._where(cond, other).T
1682+
except OutOfBoundsDatetime:
1683+
raise
16821684
except (ValueError, TypeError):
16831685
if self.ndim == 1 or self.shape[0] == 1:
16841686
if isinstance(self.dtype, (IntervalDtype, StringDtype)):
@@ -1746,6 +1748,8 @@ def putmask(self, mask, new) -> list[Block]:
17461748
try:
17471749
# Caller is responsible for ensuring matching lengths
17481750
values._putmask(mask, new)
1751+
except OutOfBoundsDatetime:
1752+
raise
17491753
except (TypeError, ValueError):
17501754
if self.ndim == 1 or self.shape[0] == 1:
17511755
if isinstance(self.dtype, IntervalDtype):

Diff for: pandas/tests/frame/methods/test_fillna.py

+14
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,
@@ -781,3 +783,15 @@ def test_fillna_with_none_object(test_frame, dtype):
781783
if test_frame:
782784
expected = expected.to_frame()
783785
tm.assert_equal(result, expected)
786+
787+
788+
def test_fillna_out_of_bounds_datetime():
789+
# GH#61208
790+
df = DataFrame(
791+
{"datetime": date_range("1/1/2011", periods=3, freq="h"), "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(OutOfBoundsDatetime, match=msg):
797+
df.fillna(Timestamp("0001-01-01"))

0 commit comments

Comments
 (0)