Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11521,6 +11521,15 @@ def _dict_round(df: DataFrame, decimals) -> Iterator[Series]:
def _series_round(ser: Series, decimals: int) -> Series:
if is_integer_dtype(ser.dtype) or is_float_dtype(ser.dtype):
return ser.round(decimals)
elif isinstance(ser._values, (DatetimeArray, TimedeltaArray, PeriodArray)):
# GH#57781
# TODO: also the ArrowDtype analogues?
warnings.warn(
"obj.round has no effect with datetime, timedelta, "
"or period dtypes. Use obj.dt.round(...) instead.",
UserWarning,
stacklevel=find_stack_level(),
)
return ser

nv.validate_round(args, kwargs)
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,15 @@ def round(self, decimals: int) -> Self:
Caller is responsible for validating this
"""
if not self.is_numeric or self.is_bool:
if isinstance(self.values, (DatetimeArray, TimedeltaArray, PeriodArray)):
# GH#57781
# TODO: also the ArrowDtype analogues?
warnings.warn(
"obj.round has no effect with datetime, timedelta, "
"or period dtypes. Use obj.dt.round(...) instead.",
UserWarning,
stacklevel=find_stack_level(),
)
return self.copy(deep=False)
# TODO: round only defined on BaseMaskedArray
# Series also does this, so would need to fix both places
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/frame/methods/test_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,16 @@ def test_round_mixed_type(self):
"col3": date_range("20111111", periods=4),
}
)
tm.assert_frame_equal(df.round(), round_0)
tm.assert_frame_equal(df.round(1), df)
msg = "obj.round has no effect with datetime, timedelta, or period dtypes"
with tm.assert_produces_warning(UserWarning, match=msg):
tm.assert_frame_equal(df.round(), round_0)
with tm.assert_produces_warning(UserWarning, match=msg):
tm.assert_frame_equal(df.round(1), df)
tm.assert_frame_equal(df.round({"col1": 1}), df)
tm.assert_frame_equal(df.round({"col1": 0}), round_0)
tm.assert_frame_equal(df.round({"col1": 0, "col2": 1}), round_0)
tm.assert_frame_equal(df.round({"col3": 1}), df)
with tm.assert_produces_warning(UserWarning, match=msg):
tm.assert_frame_equal(df.round({"col3": 1}), df)

def test_round_with_duplicate_columns(self):
# GH#11611
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ def test_to_datetime_unit_fractional_seconds(self):
dtype="M8[ns]",
)
# GH20455 argument will incur floating point errors but no premature rounding
result = result.round("ms")
result = result.dt.round("ms")
tm.assert_series_equal(result, expected)

def test_to_datetime_unit_na_values(self):
Expand Down
Loading