Skip to content

Commit 98ed54d

Browse files
TomAugspurgerKiv
authored andcommitted
BUG: Fixed pd.unique on array of tuples (pandas-dev#16543)
1 parent b9febe0 commit 98ed54d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/source/whatsnew/v0.20.2.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ Bug Fixes
4444
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
4545
- Passing an invalid engine to :func:`read_csv` now raises an informative
4646
``ValueError`` rather than ``UnboundLocalError``. (:issue:`16511`)
47-
48-
47+
- Bug in :func:`unique` on an array of tuples (:issue:`16519`)
4948

5049

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

pandas/core/algorithms.py

+6-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
@@ -328,6 +328,11 @@ def unique(values):
328328
[b, a, c]
329329
Categories (3, object): [a < b < c]
330330
331+
An array of tuples
332+
333+
>>> pd.unique([('a', 'b'), ('b', 'a'), ('a', 'c'), ('b', 'a')])
334+
array([('a', 'b'), ('b', 'a'), ('a', 'c')], dtype=object)
335+
331336
See Also
332337
--------
333338
pandas.Index.unique

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)