-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Raise OutOfBoundsDatetime in DataFrame.replace when value exceeds datetime64[ns] bounds (GH#61671) #61717
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
ensure_str, | ||
is_bool, | ||
is_complex, | ||
is_datetime64_dtype, | ||
is_float, | ||
is_integer, | ||
is_object_dtype, | ||
|
@@ -1358,6 +1359,24 @@ def find_result_type(left_dtype: DtypeObj, right: Any) -> DtypeObj: | |
dtype, _ = infer_dtype_from(right) | ||
new_dtype = find_common_type([left_dtype, dtype]) | ||
|
||
# special case: datetime64[ns] inferred but the value may be out of bounds | ||
if ( | ||
is_datetime64_dtype(new_dtype) | ||
and lib.is_scalar(right) | ||
and isinstance(right, (np.datetime64, dt.datetime, Timestamp)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the Timestamp check is redundant bc it is a subclass of datetime |
||
): | ||
try: | ||
ts = Timestamp(right) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think |
||
casted = np.datetime64(ts, "ns") | ||
if Timestamp(casted) != ts: | ||
raise OutOfBoundsDatetime( | ||
f"{right!r} overflows datetime64[ns] during dtype inference" | ||
) | ||
except (OverflowError, ValueError) as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick can you call this |
||
raise OutOfBoundsDatetime( | ||
f"Cannot safely store {right!r} in inferred dtype 'datetime64[ns]'" | ||
) from e | ||
|
||
return new_dtype | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1430,6 +1430,18 @@ def test_replace_with_nil_na(self): | |
result = ser.replace("nil", "anything else") | ||
tm.assert_frame_equal(expected, result) | ||
|
||
def test_replace_outofbounds_datetime_raises(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you parametrize this over tz=[None, "US/Eastern"]? |
||
# GH 61671 | ||
df = DataFrame([np.nan], dtype="datetime64[ns]") | ||
too_big = datetime(3000, 1, 1) | ||
from pandas.errors import OutOfBoundsDatetime | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this import go at the top of the file |
||
|
||
with pytest.raises( | ||
OutOfBoundsDatetime, | ||
match="Cannot safely store .* in inferred dtype 'datetime64\\[ns\\]'", | ||
): | ||
df.replace(np.nan, too_big) | ||
|
||
|
||
class TestDataFrameReplaceRegex: | ||
@pytest.mark.parametrize( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add something like "# GH#61671" here