Skip to content

Commit e807bb7

Browse files
committed
Fixed Issue#63444, round handles empty series now
1 parent 2d6c51f commit e807bb7

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

pandas/core/series.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2579,8 +2579,11 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series:
25792579
dtype: float64
25802580
"""
25812581
nv.validate_round(args, kwargs)
2582-
if self.dtype == "object":
2582+
if not self.empty and (self.dtype == "object" or self.dtype=='str'):
25832583
raise TypeError("Expected numeric dtype, got object instead.")
2584+
if not self.empty and (self.dtype=='str'):
2585+
raise TypeError("Expected numeric dtype, got string instead.")
2586+
25842587
new_mgr = self._mgr.round(decimals=decimals)
25852588
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
25862589
self, method="round"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
import numpy as np
3+
import pandas as pd
4+
import pandas.testing as tm
5+
6+
#GH63444
7+
8+
9+
def test_series_round_empty1():
10+
result = pd.Series([]).round(2)
11+
expected = pd.Series([])
12+
13+
def test_series_round_empty2():
14+
result = pd.Series([], dtype='float64').round()
15+
expected = pd.Series([], dtype='float64')
16+
tm.assert_series_equal(result, expected)
17+
def test_series_round_numeric():
18+
result = pd.Series([1.2345, 2.6789, 3.14159]).round(2)
19+
expected = pd.Series([1.23, 2.68, 3.14])
20+
tm.assert_series_equal(result, expected)
21+
def test_series_round_integers():
22+
result = pd.Series([1, 2, 3]).round(2)
23+
expected = pd.Series([1, 2, 3])
24+
tm.assert_series_equal(result, expected)
25+
26+
def test_series_round_str():
27+
with pytest.raises(TypeError):
28+
pd.Series(['a', 'b', 'c']).round(2)
29+
def test_series_round_obj():
30+
with pytest.raises(TypeError):
31+
pd.Series(['123'], dtype='object').round(2)

0 commit comments

Comments
 (0)