Skip to content

BUG: clip doesn't preserve dtype by column #24458

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

Merged
merged 6 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 10 additions & 17 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7132,28 +7132,21 @@ def _clip_with_scalar(self, lower, upper, inplace=False):
(upper is not None and np.any(isna(upper)))):
raise ValueError("Cannot use an NA value as a clip threshold")

result = self.values
mask = isna(result)

with np.errstate(all='ignore'):
if upper is not None:
result = np.where(result >= upper, upper, result)
if lower is not None:
result = np.where(result <= lower, lower, result)
if np.any(mask):
result[mask] = np.nan
result = self

axes_dict = self._construct_axes_dict()
result = self._constructor(result, **axes_dict).__finalize__(self)
if upper is not None:
subset = self.le(upper, axis=None) | isna(result)
result = result.where(subset, upper, axis=None, inplace=inplace)
if lower is not None:
if inplace:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this inplace check only when lower is not None? It looks like you also assign result = self before the upper and lower check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mroeschke, thanks for reviewing!

result = self is to ensure there is a return value in case both lower and upper are None, which they can. when inplace=True, result is None

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its just better to use the existing idiom

if inplace:
     self._update_inplace(result0
else:
    return self

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actioned. thanks.

result = self
subset = self.ge(lower, axis=None) | isna(result)
result = result.where(subset, lower, axis=None, inplace=inplace)

if inplace:
self._update_inplace(result)
else:
return result
return result

def _clip_with_one_bound(self, threshold, method, axis, inplace):

inplace = validate_bool_kwarg(inplace, 'inplace')
if axis is not None:
axis = self._get_axis_number(axis)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,8 @@ def test_clip_mixed_numeric(self):
df = DataFrame({'A': [1, 2, 3],
'B': [1., np.nan, 3.]})
result = df.clip(1, 2)
expected = DataFrame({'A': [1, 2, 2.],
# GH 24162, clipping now preserves types
expected = DataFrame({'A': [1, 2, 2],
'B': [1., np.nan, 2.]})
tm.assert_frame_equal(result, expected, check_like=True)

Expand Down