Skip to content

Commit 4f1c1dc

Browse files
cgangwar11jreback
authored andcommitted
1 parent ff28048 commit 4f1c1dc

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,7 @@ Conversion
14481448

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

14521453
Strings
14531454
^^^^^^^

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7176,7 +7176,7 @@ def _clip_with_one_bound(self, threshold, method, axis, inplace):
71767176
if isinstance(self, ABCSeries):
71777177
threshold = pd.Series(threshold, index=self.index)
71787178
else:
7179-
threshold = _align_method_FRAME(self, np.asarray(threshold),
7179+
threshold = _align_method_FRAME(self, threshold,
71807180
axis)
71817181
return self.where(subset, threshold, axis=axis, inplace=inplace)
71827182

pandas/tests/frame/test_analytics.py

+16
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,22 @@ def test_clip_against_frame(self, axis):
19711971
tm.assert_frame_equal(clipped_df[ub_mask], ub[ub_mask])
19721972
tm.assert_frame_equal(clipped_df[mask], df[mask])
19731973

1974+
def test_clip_against_unordered_columns(self):
1975+
# GH 20911
1976+
df1 = DataFrame(np.random.randn(1000, 4), columns=['A', 'B', 'C', 'D'])
1977+
df2 = DataFrame(np.random.randn(1000, 4), columns=['D', 'A', 'B', 'C'])
1978+
df3 = DataFrame(df2.values - 1, columns=['B', 'D', 'C', 'A'])
1979+
result_upper = df1.clip(lower=0, upper=df2)
1980+
expected_upper = df1.clip(lower=0, upper=df2[df1.columns])
1981+
result_lower = df1.clip(lower=df3, upper=3)
1982+
expected_lower = df1.clip(lower=df3[df1.columns], upper=3)
1983+
result_lower_upper = df1.clip(lower=df3, upper=df2)
1984+
expected_lower_upper = df1.clip(lower=df3[df1.columns],
1985+
upper=df2[df1.columns])
1986+
tm.assert_frame_equal(result_upper, expected_upper)
1987+
tm.assert_frame_equal(result_lower, expected_lower)
1988+
tm.assert_frame_equal(result_lower_upper, expected_lower_upper)
1989+
19741990
def test_clip_with_na_args(self, float_frame):
19751991
"""Should process np.nan argument as None """
19761992
# GH 17276

0 commit comments

Comments
 (0)