Skip to content

Preserve None in Series unique #20893

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 6 commits into from
May 11, 2018
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
7 changes: 6 additions & 1 deletion pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,12 @@ cdef class PyObjectHashTable(HashTable):
for i in range(n):
val = values[i]
hash(val)
if not checknull(val):

# `val is None` below is exception to prevent mangling of None and
# other NA values; note however that other NA values (ex: pd.NaT
# and np.nan) will still get mangled, so many not be a permanent
# solution; see GH 20866
if not checknull(val) or val is None:
k = kh_get_pymap(self.table, <PyObject*>val)
if k == self.table.n_buckets:
kh_put_pymap(self.table, <PyObject*>val, &ret)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ def test_tuple_with_strings(self, arg, expected):
result = pd.unique(arg)
tm.assert_numpy_array_equal(result, expected)

def test_obj_none_preservation(self):
# GH 20866
arr = np.array(['foo', None], dtype=object)
result = pd.unique(arr)
expected = np.array(['foo', None], dtype=object)

tm.assert_numpy_array_equal(result, expected, strict_nan=True)


class TestIsin(object):

Expand Down