Skip to content

BUG: Raise TypeError only if key DataFrame is not empty #10126 #10196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.16.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave from here on

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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still need an assert_frame_equal here to make that the assignment is correct.

def test_delitem_corner(self):
f = self.frame.copy()
del f['D']
Expand Down Expand Up @@ -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))

Expand All @@ -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')])
Expand Down