-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DataFrame.where does not respect axis parameter when shape is symmetric #9838
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10046,6 +10046,110 @@ def test_where_complex(self): | |
df[df.abs() >= 5] = np.nan | ||
assert_frame_equal(df,expected) | ||
|
||
def test_where_axis(self): | ||
# GH 9736 | ||
df = DataFrame(np.random.randn(2, 2)) | ||
mask = DataFrame([[False, False], [False, False]]) | ||
s = Series([0, 1]) | ||
|
||
expected = DataFrame([[0, 0], [1, 1]], dtype='float64') | ||
result = df.where(mask, s, axis='index') | ||
assert_frame_equal(result, expected) | ||
|
||
result = df.copy() | ||
result.where(mask, s, axis='index', inplace=True) | ||
assert_frame_equal(result, expected) | ||
|
||
expected = DataFrame([[0, 1], [0, 1]], dtype='float64') | ||
result = df.where(mask, s, axis='columns') | ||
assert_frame_equal(result, expected) | ||
|
||
result = df.copy() | ||
result.where(mask, s, axis='columns', inplace=True) | ||
assert_frame_equal(result, expected) | ||
|
||
# Upcast needed | ||
df = DataFrame([[1, 2], [3, 4]], dtype='int64') | ||
mask = DataFrame([[False, False], [False, False]]) | ||
s = Series([0, np.nan]) | ||
|
||
expected = DataFrame([[0, 0], [np.nan, np.nan]], dtype='float64') | ||
result = df.where(mask, s, axis='index') | ||
assert_frame_equal(result, expected) | ||
|
||
result = df.copy() | ||
result.where(mask, s, axis='index', inplace=True) | ||
assert_frame_equal(result, expected) | ||
|
||
expected = DataFrame([[0, np.nan], [0, np.nan]], dtype='float64') | ||
result = df.where(mask, s, axis='columns') | ||
assert_frame_equal(result, expected) | ||
|
||
expected = DataFrame({0 : np.array([0, 0], dtype='int64'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a test that has several dtypes, (we may have somewhere else), but for completeness. e.g. something like:
|
||
1 : np.array([np.nan, np.nan], dtype='float64')}) | ||
result = df.copy() | ||
result.where(mask, s, axis='columns', inplace=True) | ||
assert_frame_equal(result, expected) | ||
|
||
# Multiple dtypes (=> multiple Blocks) | ||
df = pd.concat([DataFrame(np.random.randn(10, 2)), | ||
DataFrame(np.random.randint(0, 10, size=(10, 2)))], | ||
ignore_index=True, axis=1) | ||
mask = DataFrame(False, columns=df.columns, index=df.index) | ||
s1 = Series(1, index=df.columns) | ||
s2 = Series(2, index=df.index) | ||
|
||
result = df.where(mask, s1, axis='columns') | ||
expected = DataFrame(1.0, columns=df.columns, index=df.index) | ||
expected[2] = expected[2].astype(int) | ||
expected[3] = expected[3].astype(int) | ||
assert_frame_equal(result, expected) | ||
|
||
result = df.copy() | ||
result.where(mask, s1, axis='columns', inplace=True) | ||
assert_frame_equal(result, expected) | ||
|
||
result = df.where(mask, s2, axis='index') | ||
expected = DataFrame(2.0, columns=df.columns, index=df.index) | ||
expected[2] = expected[2].astype(int) | ||
expected[3] = expected[3].astype(int) | ||
assert_frame_equal(result, expected) | ||
|
||
result = df.copy() | ||
result.where(mask, s2, axis='index', inplace=True) | ||
assert_frame_equal(result, expected) | ||
|
||
# DataFrame vs DataFrame | ||
d1 = df.copy().drop(1, axis=0) | ||
expected = df.copy() | ||
expected.loc[1, :] = np.nan | ||
|
||
result = df.where(mask, d1) | ||
assert_frame_equal(result, expected) | ||
result = df.where(mask, d1, axis='index') | ||
assert_frame_equal(result, expected) | ||
result = df.copy() | ||
result.where(mask, d1, inplace=True) | ||
assert_frame_equal(result, expected) | ||
result = df.copy() | ||
result.where(mask, d1, inplace=True, axis='index') | ||
assert_frame_equal(result, expected) | ||
|
||
d2 = df.copy().drop(1, axis=1) | ||
expected = df.copy() | ||
expected.loc[:, 1] = np.nan | ||
|
||
result = df.where(mask, d2) | ||
assert_frame_equal(result, expected) | ||
result = df.where(mask, d2, axis='columns') | ||
assert_frame_equal(result, expected) | ||
result = df.copy() | ||
result.where(mask, d2, inplace=True) | ||
assert_frame_equal(result, expected) | ||
result = df.copy() | ||
result.where(mask, d2, inplace=True, axis='columns') | ||
assert_frame_equal(result, expected) | ||
|
||
def test_mask(self): | ||
df = DataFrame(np.random.randn(5, 3)) | ||
cond = df > 0 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isnt'
axis=0
as the default, thenalign = axis == 0
simpler?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to keep the original behavior. Your suggestion might cause an extra re-indexing if
axis
is set explictly to 0, but I can't find any situation where it gives a different result.EDIT: Actually, it looks like both of these options are just wrong, and break in some cases where the
DataFrame
has multiple blocks. I'll have to take a closer look.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you added this note a while back. can you confirm what is going on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the comment below:
"The original behavior was to align cond and other with each Block along the 'columns' axis only when no axis parameter was passed to where (which can only happen in the case that other is also a DataFrame), but that doesn't work in some cases. The correct behavior seems to be: always align cond (because each Block only contains a subset of the columns), and align other whenever it has the same ndim as self, or if the alignment axis is 'columns': basically, every case except DataFrame.where(cond, Series, axis='index')
This is all fixed in the most recent commit."