Skip to content

Commit c2cb4da

Browse files
committed
BUG: Raise TypeError only if key DataFrame is not empty #10126
1 parent 0aceb38 commit c2cb4da

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2151,7 +2151,7 @@ def _setitem_array(self, key, value):
21512151
def _setitem_frame(self, key, value):
21522152
# support boolean setting with DataFrame input, e.g.
21532153
# df[df > df2] = 0
2154-
if key.values.dtype != np.bool_:
2154+
if key.values.size and not com.is_bool_dtype(key.values):
21552155
raise TypeError('Must pass DataFrame with boolean values only')
21562156

21572157
self._check_inplace_setting(value)

pandas/tests/test_frame.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,19 @@ def test_setitem_empty(self):
794794
result.loc[result.b.isnull(), 'a'] = result.a
795795
assert_frame_equal(result, df)
796796

797+
def test_setitem_empty_frame_with_boolean(self):
798+
# Test for issue #10126
799+
800+
for dtype in ('float', 'int64'):
801+
for df in [
802+
pd.DataFrame(dtype=dtype),
803+
pd.DataFrame(dtype=dtype, index=[1]),
804+
pd.DataFrame(dtype=dtype, columns=['A']),
805+
]:
806+
df2 = df.copy()
807+
df[df > df2] = 47
808+
assert_frame_equal(df2, df2)
809+
797810
def test_delitem_corner(self):
798811
f = self.frame.copy()
799812
del f['D']
@@ -2821,7 +2834,7 @@ def custom_frame_function(self):
28212834
data = {'col1': range(10),
28222835
'col2': range(10)}
28232836
cdf = CustomDataFrame(data)
2824-
2837+
28252838
# Did we get back our own DF class?
28262839
self.assertTrue(isinstance(cdf, CustomDataFrame))
28272840

@@ -2833,7 +2846,7 @@ def custom_frame_function(self):
28332846
# Do we get back our own DF class after slicing row-wise?
28342847
cdf_rows = cdf[1:5]
28352848
self.assertTrue(isinstance(cdf_rows, CustomDataFrame))
2836-
self.assertEqual(cdf_rows.custom_frame_function(), 'OK')
2849+
self.assertEqual(cdf_rows.custom_frame_function(), 'OK')
28372850

28382851
# Make sure sliced part of multi-index frame is custom class
28392852
mcol = pd.MultiIndex.from_tuples([('A', 'A'), ('A', 'B')])

0 commit comments

Comments
 (0)