From 7a1dcdd6cc3442c6b2d3f8012ef2a934957b6c52 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 28 Jul 2017 22:28:10 -0700 Subject: [PATCH 1/2] BUG: Allow pd.unique to accept tuple of strings --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/algorithms.py | 2 ++ pandas/tests/test_algos.py | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 0025f8d098d81..2381fb7642b65 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -327,3 +327,4 @@ Other ^^^^^ - Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`) - Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`) +- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 4ca658b35a276..f2359f3ff1a9d 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -170,6 +170,8 @@ def _ensure_arraylike(values): ABCIndexClass, ABCSeries)): inferred = lib.infer_dtype(values) if inferred in ['mixed', 'string', 'unicode']: + if isinstance(values, tuple): + values = list(values) values = lib.list_to_object_array(values) else: values = np.asarray(values) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 0e86ec123efea..53616fa7775d5 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -415,6 +415,14 @@ def test_order_of_appearance(self): expected = pd.Categorical(list('abc')) tm.assert_categorical_equal(result, expected) + @pytest.mark.parametrize("arg ,expected", [ + (('1', '1', '2'), np.array(['1', '2'], dtype=object)), + (('foo',), np.array(['foo'], dtype=object)) + ]) + def test_tuple_with_strings(self, arg, expected): + result = pd.unique(arg) + tm.assert_numpy_array_equal(result, expected) + class TestIsin(object): From 822a4dc514e940ca805bae655e67c8da3b86314a Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 29 Jul 2017 23:13:15 -0700 Subject: [PATCH 2/2] Add GH issue number --- doc/source/whatsnew/v0.21.0.txt | 2 +- pandas/tests/test_algos.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 2381fb7642b65..2d55144848e42 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -327,4 +327,4 @@ Other ^^^^^ - Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`) - Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`) -- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` +- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 53616fa7775d5..b26089ea7a822 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -420,6 +420,7 @@ def test_order_of_appearance(self): (('foo',), np.array(['foo'], dtype=object)) ]) def test_tuple_with_strings(self, arg, expected): + # see GH 17108 result = pd.unique(arg) tm.assert_numpy_array_equal(result, expected)