Skip to content

Commit 17cc098

Browse files
committed
REGR: assure .unique of mixed strings does not stringize
closes pandas-dev#16107
1 parent f8b25c2 commit 17cc098

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

pandas/core/algorithms.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ def _ensure_arraylike(values):
161161
"""
162162
if not isinstance(values, (np.ndarray, ABCCategorical,
163163
ABCIndexClass, ABCSeries)):
164-
values = np.array(values)
164+
inferred = lib.infer_dtype(values)
165+
if inferred in ['mixed', 'string', 'unicode']:
166+
values = np.asarray(values, dtype=object)
167+
else:
168+
values = np.asarray(values)
165169
return values
166170

167171

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def test_get_unique_index(self):
323323

324324
idx = ind[[0] * 5]
325325
idx_unique = ind[[0]]
326+
326327
# We test against `idx_unique`, so first we make sure it's unique
327328
# and doesn't contain nans.
328329
self.assertTrue(idx_unique.is_unique)
@@ -336,7 +337,6 @@ def test_get_unique_index(self):
336337
tm.assert_index_equal(result, idx_unique)
337338

338339
# nans:
339-
340340
if not ind._can_hold_na:
341341
continue
342342

pandas/tests/test_algos.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ def test_uint64_overflow(self):
387387
exp = np.array([1, 2, 2**63], dtype=np.uint64)
388388
tm.assert_numpy_array_equal(algos.unique(s), exp)
389389

390+
def test_nan_in_object_array(self):
391+
l = ['a', np.nan, 'c', 'c']
392+
result = pd.unique(l)
393+
expected = np.array(['a', np.nan, 'c'], dtype=object)
394+
tm.assert_numpy_array_equal(result, expected)
395+
390396
def test_categorical(self):
391397

392398
# we are expecting to return in the order
@@ -1378,8 +1384,8 @@ def test_no_mode(self):
13781384
exp = Series([], dtype=np.float64)
13791385
tm.assert_series_equal(algos.mode([]), exp)
13801386

1381-
# GH 15714
13821387
def test_mode_single(self):
1388+
# GH 15714
13831389
exp_single = [1]
13841390
data_single = [1]
13851391

0 commit comments

Comments
 (0)