Skip to content

Warn on boolean frame indexer #39373

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

Closed
wants to merge 10 commits into from
5 changes: 5 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3043,6 +3043,11 @@ def __getitem__(self, key):

# Do we have a (boolean) DataFrame?
if isinstance(key, DataFrame):
if not (key.index.equals(self.index) and key.columns.equals(self.columns)):
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure is equals is the right thing to do here.

idx = Index([1, 2, 3])

idx2 = Index([3, 2, 1])

idx.equals(idx2)

This returns False but we can certainly align bot indexes

Copy link
Author

@gooney47 gooney47 Jan 24, 2021

Choose a reason for hiding this comment

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

I agree that they are alignable. I thought the idea was to not accept alignables anymore. I just wanted to make an exception were alignables are accepted if they are already aligned, so that stuff like df[df > 0] is still possible. For every unaligned boolean frame indexer the warning refers to where.

warnings.warn("Unaligned DataFrame indexer is deprecated"
" and will be disallowed in future. Use where instead.",
FutureWarning,
stacklevel=2)
return self.where(key)

# Do we have a (boolean) 1d indexer?
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,21 @@ def test_partial_boolean_frame_indexing(self):
)
tm.assert_frame_equal(result, expected)

def test_unaligned_boolean_frame_indexing(self):
# GH 39004
df = DataFrame(
np.arange(9.0).reshape(3, 3), index=list("abc"), columns=list("ABC")
)

unaligned_indexer = DataFrame(True, index=list("ab"), columns=list("AB"))
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
df[unaligned_indexer]

aligned_indexer = DataFrame(True, index=list("abc"), columns=list("ABC"))
result = df[aligned_indexer]
expected = df
tm.assert_frame_equal(result, expected)

def test_no_reference_cycle(self):
df = DataFrame({"a": [0, 1], "b": [2, 3]})
for name in ("loc", "iloc", "at", "iat"):
Expand Down