Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: OverflowError when fillna on DataFrame with a pd.Timestamp (#61208) #61216

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,8 @@ def putmask(self, mask, new) -> list[Block]:
try:
# Caller is responsible for ensuring matching lengths
values._putmask(mask, new)
except OutOfBoundsDatetime as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except OutOfBoundsDatetime as e:
except OutOfBoundsDatetime:

raise
except (TypeError, ValueError):
if self.ndim == 1 or self.shape[0] == 1:
if isinstance(self.dtype, IntervalDtype):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
import pandas._testing as tm
from pandas.tests.frame.common import _check_mixed_float
from pandas.errors import OutOfBoundsDatetime


class TestFillNA:
Expand Down Expand Up @@ -781,3 +782,19 @@ def test_fillna_with_none_object(test_frame, dtype):
if test_frame:
expected = expected.to_frame()
tm.assert_equal(result, expected)


def test_fillna_out_of_bounds_datetime():
# GH#61208
df = DataFrame({
'datetime': date_range('1/1/2011', periods=3, freq='h'),
'value': [1, 2, 3]
})
df.iloc[0, 0] = None

msg="Cannot cast 0001-01-01 00:00:00 to unit='ns' without overflow"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set up pre-commit in the development environment to fix the format checking failures.

with pytest.raises(
OutOfBoundsDatetime,
match=msg
):
df.fillna(Timestamp('0001-01-01'), inplace=True)
Loading