-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Bug in DataFrame.drop_duplicates for empty DataFrame throws error #22394
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
Changes from 13 commits
03af0c1
79ee155
31f6099
6eb53f6
de745bb
3a5d97d
1f58a85
4f299c5
cab0958
68d69db
fb8845d
1f12545
20c03ef
fc61899
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,6 +263,22 @@ def test_drop_duplicates_tuple(): | |
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize('df', [ | ||
DataFrame(), | ||
DataFrame(columns=[]), | ||
DataFrame(columns=['A', 'B', 'C']), | ||
DataFrame(index=[]), | ||
DataFrame(index=['A', 'B', 'C']) | ||
]) | ||
def test_drop_duplicates_empty(df): | ||
# GH 20516 | ||
result = df.drop_duplicates() | ||
tm.assert_frame_equal(result, df) | ||
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 case with 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. Sure. |
||
|
||
result = df.drop_duplicates(inplace=True) | ||
tm.assert_frame_equal(result, df) | ||
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. This tests feels a bit tricky to me. So, when there are columns, we set them to an empty list. So, that would be the same as simply using as expected value But not sure if I'm missing something, but I'd expect 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. Please check the closed issue of #22409. 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. I don't think the problem may be your approach implementing this story. I agree on what @WillAyd said in #22409, but not in the side effect in this PR. I guess But the case here with And in your test you're asserting that the return will be 0 columns and 0 rows. Am I right? Does it makes sense? 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. Yeah. However, if we want to leave the column when there exists no value, then we'd need to change the 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. Oh, I understand the problem now. There is some magic on That's why your approach is not working as expected. You may try to check with |
||
|
||
|
||
def test_drop_duplicates_NA(): | ||
# none | ||
df = DataFrame({'A': [None, None, 'foo', 'bar', | ||
|
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.
instead of this, can you try to replace the last line
return self[-duplicated]
byreturn self.iloc[-duplicated]
, for the reasons I mentioned earlier?Uh oh!
There was an error while loading. Please reload this page.
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.
Then wouldn't index be lost in the case of
DataFrame(index=['A', 'B', 'C'])
? As it will beself.iloc[[]]
?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.
If we use
self.iloc[~duplicated]
, as you said, we will be able to maintain the columns if we have 4 columns and 0 rows. However, if we have 0 columns and 4 rows, we have the same problem - the outcome isself.iloc[[]]
, which means that the rows won't be selected, for the same reason with usingself[[]]
for columns, thus ending up with 0 columns and 0 rows, once again.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'm not sure whether it'd make sense for a dataframe with rows but no columns to drop the empty rows as duplicate.
But as @jreback if happy with this implementation, just leave it like this. I didn't see his comment earlier.
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 tried a test, and here's the result:
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.
Yes, that makes sense to me. If we consider that empty rows are equal among them, then
pd.DataFrame(index=['A', 'B']).duplicated()
would returnFalse, True
, and.iloc[-duplicated]
would return the first row.But as I said, I'm happy to keep the original DataFrame as is for this case, as @jreback is happy with it.
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.
Oh, if you see the empty rows as equals, then it does make sense. I haven't thought of it that way. Thanks.