From f8e7c93938eca1fa59c74131dfe3a137cc2a4d13 Mon Sep 17 00:00:00 2001 From: rekcahpassyla <0xdeadcafebeef@gmail.com> Date: Fri, 22 May 2015 10:06:46 +0100 Subject: [PATCH] BUG: Raise TypeError only if key DataFrame is not empty #10126 --- doc/source/whatsnew/v0.16.2.txt | 2 ++ pandas/core/frame.py | 2 +- pandas/tests/test_frame.py | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.16.2.txt b/doc/source/whatsnew/v0.16.2.txt index b571aab0b19a5..b409ea89a8032 100644 --- a/doc/source/whatsnew/v0.16.2.txt +++ b/doc/source/whatsnew/v0.16.2.txt @@ -83,3 +83,5 @@ Bug Fixes - Bug where infer_freq infers timerule (WOM-5XXX) unsupported by to_offset (:issue:`9425`) + +- Bug to handle masking empty ``DataFrame``(:issue:`10126`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f36108262432d..ab6f11a4b8d5b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2151,7 +2151,7 @@ def _setitem_array(self, key, value): def _setitem_frame(self, key, value): # support boolean setting with DataFrame input, e.g. # df[df > df2] = 0 - if key.values.dtype != np.bool_: + if key.values.size and not com.is_bool_dtype(key.values): raise TypeError('Must pass DataFrame with boolean values only') self._check_inplace_setting(value) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 4964d13f7ac28..f74cb07557342 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -794,6 +794,19 @@ def test_setitem_empty(self): result.loc[result.b.isnull(), 'a'] = result.a assert_frame_equal(result, df) + def test_setitem_empty_frame_with_boolean(self): + # Test for issue #10126 + + for dtype in ('float', 'int64'): + for df in [ + pd.DataFrame(dtype=dtype), + pd.DataFrame(dtype=dtype, index=[1]), + pd.DataFrame(dtype=dtype, columns=['A']), + ]: + df2 = df.copy() + df[df > df2] = 47 + assert_frame_equal(df, df2) + def test_delitem_corner(self): f = self.frame.copy() del f['D'] @@ -2821,7 +2834,7 @@ def custom_frame_function(self): data = {'col1': range(10), 'col2': range(10)} cdf = CustomDataFrame(data) - + # Did we get back our own DF class? self.assertTrue(isinstance(cdf, CustomDataFrame)) @@ -2833,7 +2846,7 @@ def custom_frame_function(self): # Do we get back our own DF class after slicing row-wise? cdf_rows = cdf[1:5] self.assertTrue(isinstance(cdf_rows, CustomDataFrame)) - self.assertEqual(cdf_rows.custom_frame_function(), 'OK') + self.assertEqual(cdf_rows.custom_frame_function(), 'OK') # Make sure sliced part of multi-index frame is custom class mcol = pd.MultiIndex.from_tuples([('A', 'A'), ('A', 'B')])