Skip to content

Commit 86606b2

Browse files
Licht-Tjreback
authored andcommitted
TST: Add list of tuples pd.factorize test (#18649)
1 parent 1a46dba commit 86606b2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pandas/tests/test_algos.py

+28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas._libs.hashtable import unique_label_indices
1818
from pandas.compat import lrange, range
1919
import pandas.core.algorithms as algos
20+
from pandas.core.common import _asarray_tuplesafe
2021
import pandas.util.testing as tm
2122
from pandas.compat.numpy import np_array_datetime64_compat
2223
from pandas.util.testing import assert_almost_equal
@@ -190,6 +191,33 @@ def test_factorize_nan(self):
190191
assert len(set(key)) == len(set(expected))
191192
tm.assert_numpy_array_equal(pd.isna(key), expected == na_sentinel)
192193

194+
@pytest.mark.parametrize("data,expected_label,expected_level", [
195+
(
196+
[(1, 1), (1, 2), (0, 0), (1, 2), 'nonsense'],
197+
[0, 1, 2, 1, 3],
198+
[(1, 1), (1, 2), (0, 0), 'nonsense']
199+
),
200+
(
201+
[(1, 1), (1, 2), (0, 0), (1, 2), (1, 2, 3)],
202+
[0, 1, 2, 1, 3],
203+
[(1, 1), (1, 2), (0, 0), (1, 2, 3)]
204+
),
205+
(
206+
[(1, 1), (1, 2), (0, 0), (1, 2)],
207+
[0, 1, 2, 1],
208+
[(1, 1), (1, 2), (0, 0)]
209+
)
210+
])
211+
def test_factorize_tuple_list(self, data, expected_label, expected_level):
212+
# GH9454
213+
result = pd.factorize(data)
214+
215+
tm.assert_numpy_array_equal(result[0],
216+
np.array(expected_label, dtype=np.intp))
217+
218+
expected_level_array = _asarray_tuplesafe(expected_level, dtype=object)
219+
tm.assert_numpy_array_equal(result[1], expected_level_array)
220+
193221
def test_complex_sorting(self):
194222
# gh 12666 - check no segfault
195223
# Test not valid numpy versions older than 1.11

0 commit comments

Comments
 (0)