Skip to content

REGR: assure .unique of mixed strings does not stringize #16108

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 1 commit into from
Apr 25, 2017
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
6 changes: 5 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ def _ensure_arraylike(values):
"""
if not isinstance(values, (np.ndarray, ABCCategorical,
ABCIndexClass, ABCSeries)):
values = np.array(values)
inferred = lib.infer_dtype(values)
if inferred in ['mixed', 'string', 'unicode']:
values = np.asarray(values, dtype=object)
else:
values = np.asarray(values)
return values


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def test_get_unique_index(self):

idx = ind[[0] * 5]
idx_unique = ind[[0]]

# We test against `idx_unique`, so first we make sure it's unique
# and doesn't contain nans.
self.assertTrue(idx_unique.is_unique)
Expand All @@ -336,7 +337,6 @@ def test_get_unique_index(self):
tm.assert_index_equal(result, idx_unique)

# nans:

if not ind._can_hold_na:
continue

Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ def test_uint64_overflow(self):
exp = np.array([1, 2, 2**63], dtype=np.uint64)
tm.assert_numpy_array_equal(algos.unique(s), exp)

def test_nan_in_object_array(self):
l = ['a', np.nan, 'c', 'c']
result = pd.unique(l)
expected = np.array(['a', np.nan, 'c'], dtype=object)
tm.assert_numpy_array_equal(result, expected)

def test_categorical(self):

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

# GH 15714
def test_mode_single(self):
# GH 15714
exp_single = [1]
data_single = [1]

Expand Down