Skip to content
Closed
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
5 changes: 5 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,11 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series:
dtype: float64
"""
nv.validate_round(args, kwargs)

# Handle empty Series gracefully
if len(self) == 0:
return self.copy()

if self.dtype == "object":
raise TypeError("Expected numeric dtype, got object instead.")
new_mgr = self._mgr.round(decimals=decimals)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/frame/methods/test_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,20 @@ def test_round_empty_not_input(self):
result = df.round()
tm.assert_frame_equal(df, result)
assert df is not result

def test_round_empty_series(self):
"""Test that round works on empty Series."""
# Empty Series with default object dtype
result = Series().round(4)
expected = Series()
tm.assert_series_equal(result, expected)

# Empty Series with float dtype
result = Series(dtype="float64").round(4)
expected = Series(dtype="float64")
tm.assert_series_equal(result, expected)

# Empty Series with int dtype
result = Series(dtype="int64").round(4)
expected = Series(dtype="int64")
tm.assert_series_equal(result, expected)
Loading