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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ Other
array if the array shares data with the original DataFrame or Series (:ref:`copy_on_write_read_only_na`).
This logic is expanded to accessing the underlying pandas ExtensionArray
through ``.array`` (or ``.values`` depending on the dtype) as well (:issue:`61925`).

- Bug in :meth:`Series.round` where series shows TypeError when passed empty Series (:issue:`63444`)
.. ***DO NOT USE THIS SECTION***
.. ---------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2579,8 +2579,11 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series:
dtype: float64
"""
nv.validate_round(args, kwargs)
if self.dtype == "object":
if not self.empty and (self.dtype == "object" or self.dtype == "str"):
raise TypeError("Expected numeric dtype, got object instead.")
if not self.empty and (self.dtype == "str"):
raise TypeError("Expected numeric dtype, got string instead.")

new_mgr = self._mgr.round(decimals=decimals)
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
self, method="round"
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/series/methods/test_round_empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest

import pandas as pd
import pandas.testing as tm

# GH63444


def test_series_round_empty1():
result = pd.Series([]).round(2)
expected = pd.Series([])
tm.assert_series_equal(result, expected)


def test_series_round_empty2():
result = pd.Series([], dtype="float64").round()
expected = pd.Series([], dtype="float64")
tm.assert_series_equal(result, expected)


def test_series_round_numeric():
result = pd.Series([1.2345, 2.6789, 3.14159]).round(2)
expected = pd.Series([1.23, 2.68, 3.14])
tm.assert_series_equal(result, expected)


def test_series_round_integers():
result = pd.Series([1, 2, 3]).round(2)
expected = pd.Series([1, 2, 3])
tm.assert_series_equal(result, expected)


def test_series_round_str():
with pytest.raises(TypeError):
pd.Series(["a", "b", "c"]).round(2)


def test_series_round_obj():
with pytest.raises(TypeError):
pd.Series(["123"], dtype="object").round(2)
Loading