Skip to content

Commit

Permalink
Bug : Fixes #20911 (#24467)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgangwar11 authored and jreback committed Dec 28, 2018
1 parent ff28048 commit 4f1c1dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ Conversion

- Bug in :meth:`DataFrame.combine_first` in which column types were unexpectedly converted to float (:issue:`20699`)
- Bug in :meth:`DataFrame.clip` in which column types are not preserved and casted to float (:issue:`24162`)
- Bug in :meth:`DataFrame.clip` when order of columns of dataframes doesn't match, result observed is wrong in numeric values (:issue:`20911`)

Strings
^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7176,7 +7176,7 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
if isinstance(self, ABCSeries):
threshold = pd.Series(threshold, index=self.index)
else:
threshold = _align_method_FRAME(self, np.asarray(threshold),
threshold = _align_method_FRAME(self, threshold,
axis)
return self.where(subset, threshold, axis=axis, inplace=inplace)

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,22 @@ def test_clip_against_frame(self, axis):
tm.assert_frame_equal(clipped_df[ub_mask], ub[ub_mask])
tm.assert_frame_equal(clipped_df[mask], df[mask])

def test_clip_against_unordered_columns(self):
# GH 20911
df1 = DataFrame(np.random.randn(1000, 4), columns=['A', 'B', 'C', 'D'])
df2 = DataFrame(np.random.randn(1000, 4), columns=['D', 'A', 'B', 'C'])
df3 = DataFrame(df2.values - 1, columns=['B', 'D', 'C', 'A'])
result_upper = df1.clip(lower=0, upper=df2)
expected_upper = df1.clip(lower=0, upper=df2[df1.columns])
result_lower = df1.clip(lower=df3, upper=3)
expected_lower = df1.clip(lower=df3[df1.columns], upper=3)
result_lower_upper = df1.clip(lower=df3, upper=df2)
expected_lower_upper = df1.clip(lower=df3[df1.columns],
upper=df2[df1.columns])
tm.assert_frame_equal(result_upper, expected_upper)
tm.assert_frame_equal(result_lower, expected_lower)
tm.assert_frame_equal(result_lower_upper, expected_lower_upper)

def test_clip_with_na_args(self, float_frame):
"""Should process np.nan argument as None """
# GH 17276
Expand Down

0 comments on commit 4f1c1dc

Please sign in to comment.