Repository: pandas-dev/pandas
Pull request: Bug : Fixes #20911
- [x] closes #20911
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

**Before**
```python
In [1]: import pandas as pd                                                                   

In [2]: df1 = pd.DataFrame([[1., 0.], [3., 0.]], columns=['A', 'B']) 
   ...:                                                                                       

In [3]: df2 = pd.DataFrame([[100., 1.], [100., 2.]], columns=['B', 'A']) 
   ...:                                                                                       

In [4]: df1                                                                                   
Out[4]: 
     A    B
0  1.0  0.0
1  3.0  0.0

In [5]: df2                                                                                   
Out[5]: 
       B    A
0  100.0  1.0
1  100.0  2.0

In [6]: df1.clip(lower=0,upper=df2)                                                           
Out[6]: 
       A    B
0    1.0  0.0
1  100.0  0.0

```
**After**
```python
In [5]: df1.clip(lower=0,upper=df2)                                                           
Out[5]: 
     A    B
0  1.0  0.0
1  2.0  0.0


```


Problem statement:
DataFrame.clip() bug when bound a frame when columns not sorted
#### Code Sample, a copy-pastable example if possible

```python
In [14]: import pandas as pd

In [15]: df1 = pd.DataFrame([[1., 0.], [3., 0.]], columns=['A', 'B'])

In [16]: df2 = pd.DataFrame([[100., 1.], [100., 2.]], columns=['B', 'A'])

In [17]: df1.clip(lower=0, upper=df2)
Out[17]:
       A    B
0    1.0  0.0
1  100.0  0.0
In [18]: df1
Out[18]:
     A    B
0  1.0  0.0
1  3.0  0.0

# selecting columns in order produces the correct results
In [20]: df1.clip(lower=0, upper=df2[df1.columns])
Out[20]:
     A    B
0  1.0  0.0
1  2.0  0.0


```
#### Problem description

The behavior was correct in 0.18.1.  df1.A = 3.0 is being clipped to 100.0.  This not expected.

#### Expected Output
```python
In [39]: df1.clip(lower=0, upper=df2)
Out[39]:
     A    B
0  1.0  0.0
1  2.0  0.0
```
#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.14.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-327.36.3.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: C
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.22.0
pytest: 3.5.0
pip: 9.0.3
setuptools: 39.0.1
Cython: 0.28.2
numpy: 1.14.2
scipy: 1.0.1
pyarrow: 0.9.0
xarray: 0.10.2
IPython: 5.6.0
sphinx: 1.7.2
patsy: 0.5.0
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.2
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.2.1
bs4: 4.3.2
html5lib: 0.999
sqlalchemy: 1.2.6
pymysql: None
psycopg2: 2.7.4 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None


</details>

Hints from issue discussion:

Commit message: TST GH20911

Additional test cases to validate the fix

Patch to review:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 65b219cc57f3a..ce7a866496964 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -7172,7 +7172,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)
 
diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py
index 88262220015c7..687fcbcd5105b 100644
--- a/pandas/tests/frame/test_analytics.py
+++ b/pandas/tests/frame/test_analytics.py
@@ -1964,6 +1964,13 @@ def test_clip_against_frame(self, axis):
         tm.assert_frame_equal(clipped_df[lb_mask], lb[lb_mask])
         tm.assert_frame_equal(clipped_df[ub_mask], ub[ub_mask])
         tm.assert_frame_equal(clipped_df[mask], df[mask])
+         # GH 20911, clipping now preserves types       
+        df1 = DataFrame(np.random.randn(1000,4), columns=['A', 'B','C','D'])
+        df2 = DataFrame(np.random.randn(1000,4), columns=['D', 'A','B','C'])
+        
+        res1 = df1.clip(lower=0,upper=df2)
+        res2 = df1.clip(lower=0,upper=df2[df1.columns])
+        tm.assert_frame_equal(res1,res2)
 
     def test_clip_with_na_args(self, float_frame):
         """Should process np.nan argument as None """


Code review comments:
diff_hunk: @@ -1964,6 +1964,13 @@ def test_clip_against_frame(self, axis):
         tm.assert_frame_equal(clipped_df[lb_mask], lb[lb_mask])
         tm.assert_frame_equal(clipped_df[ub_mask], ub[ub_mask])
         tm.assert_frame_equal(clipped_df[mask], df[mask])
+         # GH 20911, clipping now preserves types       
+        df1 = DataFrame(np.random.randn(1000,4), columns=['A', 'B','C','D'])
+        df2 = DataFrame(np.random.randn(1000,4), columns=['D', 'A','B','C'])
+        
+        res1 = df1.clip(lower=0,upper=df2)
path: pandas/tests/frame/test_analytics.py
text: @user1:
use result= and expected=

@author:
will do pardon me for such a rookie mistake....

diff_hunk: @@ -1964,6 +1964,13 @@ def test_clip_against_frame(self, axis):
         tm.assert_frame_equal(clipped_df[lb_mask], lb[lb_mask])
         tm.assert_frame_equal(clipped_df[ub_mask], ub[ub_mask])
         tm.assert_frame_equal(clipped_df[mask], df[mask])
+         # GH 20911, clipping now preserves types       
+        df1 = DataFrame(np.random.randn(1000,4), columns=['A', 'B','C','D'])
+        df2 = DataFrame(np.random.randn(1000,4), columns=['D', 'A','B','C'])
path: pandas/tests/frame/test_analytics.py
text: @user1:
you have some lint errors.