Skip to content

Commit 35da5b9

Browse files
committed
BUG: Fixed pd.unique on array of tuples
Closes pandas-dev#16519
1 parent e60dc4c commit 35da5b9

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.20.2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Bug Fixes
3939

4040
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
4141
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
42-
42+
- Bug in :func:`pd.unique` on an array of tuples (:issue:`16519`)
4343

4444

4545
- Fixed a compatibility issue with IPython 6.0's tab completion showing deprecation warnings on Categoricals (:issue:`16409`)

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def _ensure_arraylike(values):
163163
ABCIndexClass, ABCSeries)):
164164
inferred = lib.infer_dtype(values)
165165
if inferred in ['mixed', 'string', 'unicode']:
166-
values = np.asarray(values, dtype=object)
166+
values = lib.list_to_object_array(values)
167167
else:
168168
values = np.asarray(values)
169169
return values

pandas/tests/test_algos.py

+16
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,22 @@ def test_unique_index(self):
929929
tm.assert_numpy_array_equal(case.duplicated(),
930930
np.array([False, False, False]))
931931

932+
@pytest.mark.parametrize('arr, unique', [
933+
([(0, 0), (0, 1), (1, 0), (1, 1), (0, 0), (0, 1), (1, 0), (1, 1)],
934+
[(0, 0), (0, 1), (1, 0), (1, 1)]),
935+
([('b', 'c'), ('a', 'b'), ('a', 'b'), ('b', 'c')],
936+
[('b', 'c'), ('a', 'b')]),
937+
([('a', 1), ('b', 2), ('a', 3), ('a', 1)],
938+
[('a', 1), ('b', 2), ('a', 3)]),
939+
])
940+
def test_unique_tuples(self, arr, unique):
941+
# https://github.com/pandas-dev/pandas/issues/16519
942+
expected = np.empty(len(unique), dtype=object)
943+
expected[:] = unique
944+
945+
result = pd.unique(arr)
946+
tm.assert_numpy_array_equal(result, expected)
947+
932948

933949
class GroupVarTestMixin(object):
934950

0 commit comments

Comments
 (0)