Repository: pandas-dev/pandas

Issue:
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>

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>



Discussion:

Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -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)
 


Test patch:
diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py
--- a/pandas/tests/frame/test_analytics.py
+++ b/pandas/tests/frame/test_analytics.py
@@ -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