-
-
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?
BUG/TST: Series round with dtype object #62174
Conversation
pandas/core/series.py
Outdated
return self._constructor( | ||
new_values, index=self.index, copy=False | ||
).__finalize__(self, method="map") | ||
except TypeError as e: |
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.
nitpick e->err. try to avoid single-letter variable names
pandas/core/series.py
Outdated
raise TypeError("Expected numeric dtype, got object instead.") | ||
try: | ||
round_func = functools.partial(round, ndigits=decimals) | ||
new_values = self._map_values(round_func) |
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
In another PR you're going to make DataFrame do the same thing this does, right? If so, you'll need to change the Block behavior. |
ser = Series([0.232], dtype="object") | ||
expected = Series([0.2]) | ||
tm.assert_series_equal(ser.round(1), expected) | ||
ser2 = Series(["bar"], dtype="object") |
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.
can you make this a separate test
# GH#61206, GH#62174 | ||
ser = Series([0.232], dtype="object") | ||
expected = Series([0.2]) | ||
tm.assert_series_equal(ser.round(1), expected) |
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.
separate line for result = ser.round(1)
i suspect this will also fix/change the DataFrame.round behavior? can you test that and address it in the whatsnew note |
pandas/core/internals/blocks.py
Outdated
round_func = functools.partial(round, ndigits=decimals) | ||
mapper = functools.partial(algos.map_array, mapper=round_func) | ||
try: | ||
if self.values.ndim == 1: | ||
values = algos.map_array(self.values, round_func) | ||
else: | ||
values = np.apply_along_axis(mapper, 0, self.values) |
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.
Definitely feels like there is a better way to account for 2d blocks but I couldn't think of a better way. Part of that was I couldn't find where in lib.map_infer()
was defined (from the return statement in algos.map_array()
) and thus didn't know why algos.map_array
wasn't working for the 2d ndarray.
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.
algos.map_array(self.values.ravel(), round_func).reshape(self.values.shape)
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.
ohh okay makes sense, thank you
pandas/core/internals/blocks.py
Outdated
@final | ||
def __repr__(self) -> str: | ||
# don't want to print out all of the items here | ||
# don't want to out all of the items here |
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.
?
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.
oops, thanks for catching that
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.I changed the behavior or series.round() to act like
series.map(round)
if the dtype is object as discussed in #61682. I put this in a new PR as the original intent of the other PR was to change DataFrame behavior. I'm unsure if the way I approached it is a good way to make the change though because I saw incore/internals/blocks.py
thatBlock
'sround()
method is intended to raise an error for dtype of object. So I wasn't sure I should approach this by updated theBlock
class instead. Any feedback is appreciated!