From 85063b8a3b0d9f8ad21d035628b4ed3563f35b73 Mon Sep 17 00:00:00 2001 From: Licht-T Date: Wed, 6 Dec 2017 08:13:59 +0900 Subject: [PATCH] TST: Add list of tuples pd.factorize test --- pandas/tests/test_algos.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index bf244deec9ffc..64564df4febe8 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -17,6 +17,7 @@ from pandas._libs.hashtable import unique_label_indices from pandas.compat import lrange, range import pandas.core.algorithms as algos +from pandas.core.common import _asarray_tuplesafe import pandas.util.testing as tm from pandas.compat.numpy import np_array_datetime64_compat from pandas.util.testing import assert_almost_equal @@ -190,6 +191,33 @@ def test_factorize_nan(self): assert len(set(key)) == len(set(expected)) tm.assert_numpy_array_equal(pd.isna(key), expected == na_sentinel) + @pytest.mark.parametrize("data,expected_label,expected_level", [ + ( + [(1, 1), (1, 2), (0, 0), (1, 2), 'nonsense'], + [0, 1, 2, 1, 3], + [(1, 1), (1, 2), (0, 0), 'nonsense'] + ), + ( + [(1, 1), (1, 2), (0, 0), (1, 2), (1, 2, 3)], + [0, 1, 2, 1, 3], + [(1, 1), (1, 2), (0, 0), (1, 2, 3)] + ), + ( + [(1, 1), (1, 2), (0, 0), (1, 2)], + [0, 1, 2, 1], + [(1, 1), (1, 2), (0, 0)] + ) + ]) + def test_factorize_tuple_list(self, data, expected_label, expected_level): + # GH9454 + result = pd.factorize(data) + + tm.assert_numpy_array_equal(result[0], + np.array(expected_label, dtype=np.intp)) + + expected_level_array = _asarray_tuplesafe(expected_level, dtype=object) + tm.assert_numpy_array_equal(result[1], expected_level_array) + def test_complex_sorting(self): # gh 12666 - check no segfault # Test not valid numpy versions older than 1.11