-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
03af0c1
Bug in :meth:`DataFrame.drop_duplicates`for empty DataFrame throws er…
HyunTruth 79ee155
fixed what's new to render well
HyunTruth 31f6099
Applied changes according to reviews by @jreback
HyunTruth 6eb53f6
removed an additional line
HyunTruth de745bb
changed test to accomodate the column behavior in selection
HyunTruth 3a5d97d
Parameterized the tests
1f58a85
Parameterized the tests
4f299c5
Merge branch 'dropdup' of https://github.com/HyunTruth/pandas into dr…
cab0958
Adhere to flake8
68d69db
switched df
fb8845d
Try catching for empty dataframes and return self
HyunTruth 1f12545
change requested applied
HyunTruth 20c03ef
added inplace=True tests
HyunTruth fc61899
rectified inplace test to reflect actual usage
HyunTruth 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,6 +263,23 @@ 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.copy() | ||
result.drop_duplicates(inplace=True) | ||
tm.assert_frame_equal(result, df) | ||
|
||
|
||
def test_drop_duplicates_NA(): | ||
# none | ||
df = DataFrame({'A': [None, None, 'foo', 'bar', | ||
|
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.
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.