Skip to content

Commit 6de48f6

Browse files
WillAydtp
authored and
tp
committed
Preserve None in Series unique (pandas-dev#20893)
1 parent 5b90236 commit 6de48f6

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/_libs/hashtable_class_helper.pxi.in

+6-1
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,12 @@ cdef class PyObjectHashTable(HashTable):
870870
for i in range(n):
871871
val = values[i]
872872
hash(val)
873-
if not checknull(val):
873+
874+
# `val is None` below is exception to prevent mangling of None and
875+
# other NA values; note however that other NA values (ex: pd.NaT
876+
# and np.nan) will still get mangled, so many not be a permanent
877+
# solution; see GH 20866
878+
if not checknull(val) or val is None:
874879
k = kh_get_pymap(self.table, <PyObject*>val)
875880
if k == self.table.n_buckets:
876881
kh_put_pymap(self.table, <PyObject*>val, &ret)

pandas/tests/test_algos.py

+8
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,14 @@ def test_tuple_with_strings(self, arg, expected):
491491
result = pd.unique(arg)
492492
tm.assert_numpy_array_equal(result, expected)
493493

494+
def test_obj_none_preservation(self):
495+
# GH 20866
496+
arr = np.array(['foo', None], dtype=object)
497+
result = pd.unique(arr)
498+
expected = np.array(['foo', None], dtype=object)
499+
500+
tm.assert_numpy_array_equal(result, expected, strict_nan=True)
501+
494502

495503
class TestIsin(object):
496504

0 commit comments

Comments
 (0)