Skip to content

BUG: drop_duplicates drops non-duplicate rows in the presence of integer columns #11403

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 2 commits into from
Oct 24, 2015
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Bug Fixes
- Bug in ``pivot_table`` with ``margins=True`` when indexes are of ``Categorical`` dtype (:issue:`10993`)
- Bug in ``DataFrame.plot`` cannot use hex strings colors (:issue:`10299`)


- Bug in ``DataFrame.drop_duplicates`` (regression from 0.16.2) causing some non-duplicate rows containing integer values to be dropped (:issue:`11376`)


- Bug in ``pd.eval`` where unary ops in a list error (:issue:`11235`)
Expand Down
8 changes: 1 addition & 7 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2994,13 +2994,7 @@ def duplicated(self, subset=None, keep='first'):
from pandas.hashtable import duplicated_int64, _SIZE_HINT_LIMIT

def f(vals):

# if we have integers we can directly index with these
if com.is_integer_dtype(vals):
from pandas.core.nanops import unique1d
labels, shape = vals, unique1d(vals)
else:
labels, shape = factorize(vals, size_hint=min(len(self), _SIZE_HINT_LIMIT))
labels, shape = factorize(vals, size_hint=min(len(self), _SIZE_HINT_LIMIT))
return labels.astype('i8',copy=False), len(shape)

if subset is None:
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8380,6 +8380,25 @@ def test_drop_duplicates(self):
expected = df.iloc[[-2,-1]]
assert_frame_equal(result, expected)

# GH 11376
df = pd.DataFrame({'x': [7, 6, 3, 3, 4, 8, 0],
'y': [0, 6, 5, 5, 9, 1, 2]})
expected = df.loc[df.index != 3]
assert_frame_equal(df.drop_duplicates(), expected)

df = pd.DataFrame([[1 , 0], [0, 2]])
assert_frame_equal(df.drop_duplicates(), df)

df = pd.DataFrame([[-2, 0], [0, -4]])
assert_frame_equal(df.drop_duplicates(), df)

Copy link
Contributor

Choose a reason for hiding this comment

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

can u add the op example here as well
do we have sufficient coverage for various dtypes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see tests for strings, integers, and floats. This particular problem happens only for integers, though.

x = np.iinfo(np.int64).max / 3 * 2
df = pd.DataFrame([[-x, x], [0, x + 4]])
assert_frame_equal(df.drop_duplicates(), df)

df = pd.DataFrame([[-x, x], [x, x + 4]])
assert_frame_equal(df.drop_duplicates(), df)

def test_drop_duplicates_for_take_all(self):
df = DataFrame({'AAA': ['foo', 'bar', 'baz', 'bar',
'foo', 'bar', 'qux', 'foo'],
Expand Down