Skip to content

BUG: DataFrame.drop_duplicates method fails when a column with a list… #56958

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 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6748,6 +6748,22 @@ def drop_duplicates(
DataFrame or None
DataFrame with duplicates removed or None if ``inplace=True``.

Notes
-------
To handle mutable objects such as list, convert the list column
to a tuple before using it in the subset.
Comment on lines +6753 to +6754
Copy link
Member

Choose a reason for hiding this comment

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

This does not handle all cases. I think the note should start with something more general like:

This method requires data in the columns to be hashable.


>>> df = pd.DataFrame([
... {'number': 1, 'item_ids': [1, 2, 3]},
... {'number': 1, 'item_ids': [1, 2, 3]},
... ])

>>> df['item_ids'] = df['item_ids'].apply(tuple)
>>> df.drop_duplicates(inplace=True)
Copy link
Member

Choose a reason for hiding this comment

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

The inplace argument should be going away (hopefully soon); it should not be used here.

>>> df['item_ids'] = df['item_ids'].apply(list)
number item_ids
0 1 [1, 2, 3]
Comment on lines +6763 to +6765
Copy link
Member

Choose a reason for hiding this comment

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

This line doesn't produce any output. If you want to show output, you can make it just df['items_ids'].apply(list) instead of assigning it back to the column.


See Also
--------
DataFrame.value_counts: Count unique combinations of columns.
Expand Down