-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
BUG/TST: Series round with dtype object #62174
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 4 commits
5f0bd7d
a919646
c44e2af
fd1f6b6
0ab7faa
83acc9e
2c3d0e2
084569f
bb50529
8226901
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 |
---|---|---|
|
@@ -2514,7 +2514,14 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series: | |
""" | ||
nv.validate_round(args, kwargs) | ||
if self.dtype == "object": | ||
raise TypeError("Expected numeric dtype, got object instead.") | ||
try: | ||
round_func = functools.partial(round, ndigits=decimals) | ||
new_values = self._map_values(round_func) | ||
return self._constructor( | ||
new_values, index=self.index, copy=False | ||
).__finalize__(self, method="map") | ||
except TypeError as e: | ||
|
||
raise TypeError("Expected numeric entries for dtype object.") from e | ||
new_mgr = self._mgr.round(decimals=decimals) | ||
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__( | ||
self, method="round" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,8 +74,11 @@ def test_round_ea_boolean(self): | |
tm.assert_series_equal(ser, expected) | ||
|
||
def test_round_dtype_object(self): | ||
# GH#61206 | ||
ser = Series([0.2], dtype="object") | ||
msg = "Expected numeric dtype, got object instead." | ||
# GH#61206, GH#62174 | ||
ser = Series([0.232], dtype="object") | ||
expected = Series([0.2]) | ||
tm.assert_series_equal(ser.round(1), expected) | ||
|
||
ser2 = Series(["bar"], dtype="object") | ||
|
||
msg = "Expected numeric entries for dtype object." | ||
with pytest.raises(TypeError, match=msg): | ||
ser.round() | ||
ser2.round() |
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.
only this line needs to go inside the try/except