Skip to content

Commit fc38f46

Browse files
authored
REF/TST: misplaced Categorical tests (#37678)
1 parent fcfaf78 commit fc38f46

File tree

7 files changed

+185
-170
lines changed

7 files changed

+185
-170
lines changed

pandas/tests/arrays/categorical/test_algos.py

-115
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,6 @@ def test_isin_cats():
5959
tm.assert_numpy_array_equal(expected, result)
6060

6161

62-
@pytest.mark.parametrize(
63-
"to_replace, value, result, expected_error_msg",
64-
[
65-
("b", "c", ["a", "c"], "Categorical.categories are different"),
66-
("c", "d", ["a", "b"], None),
67-
# https://github.com/pandas-dev/pandas/issues/33288
68-
("a", "a", ["a", "b"], None),
69-
("b", None, ["a", None], "Categorical.categories length are different"),
70-
],
71-
)
72-
def test_replace(to_replace, value, result, expected_error_msg):
73-
# GH 26988
74-
cat = pd.Categorical(["a", "b"])
75-
expected = pd.Categorical(result)
76-
result = cat.replace(to_replace, value)
77-
tm.assert_categorical_equal(result, expected)
78-
if to_replace == "b": # the "c" test is supposed to be unchanged
79-
with pytest.raises(AssertionError, match=expected_error_msg):
80-
# ensure non-inplace call does not affect original
81-
tm.assert_categorical_equal(cat, expected)
82-
cat.replace(to_replace, value, inplace=True)
83-
tm.assert_categorical_equal(cat, expected)
84-
85-
8662
@pytest.mark.parametrize("empty", [[], pd.Series(dtype=object), np.array([])])
8763
def test_isin_empty(empty):
8864
s = pd.Categorical(["a", "b"])
@@ -105,94 +81,3 @@ def test_diff():
10581
result = df.diff()
10682

10783
tm.assert_frame_equal(result, expected)
108-
109-
110-
class TestTake:
111-
# https://github.com/pandas-dev/pandas/issues/20664
112-
113-
def test_take_default_allow_fill(self):
114-
cat = pd.Categorical(["a", "b"])
115-
with tm.assert_produces_warning(None):
116-
result = cat.take([0, -1])
117-
118-
assert result.equals(cat)
119-
120-
def test_take_positive_no_warning(self):
121-
cat = pd.Categorical(["a", "b"])
122-
with tm.assert_produces_warning(None):
123-
cat.take([0, 0])
124-
125-
def test_take_bounds(self, allow_fill):
126-
# https://github.com/pandas-dev/pandas/issues/20664
127-
cat = pd.Categorical(["a", "b", "a"])
128-
if allow_fill:
129-
msg = "indices are out-of-bounds"
130-
else:
131-
msg = "index 4 is out of bounds for( axis 0 with)? size 3"
132-
with pytest.raises(IndexError, match=msg):
133-
cat.take([4, 5], allow_fill=allow_fill)
134-
135-
def test_take_empty(self, allow_fill):
136-
# https://github.com/pandas-dev/pandas/issues/20664
137-
cat = pd.Categorical([], categories=["a", "b"])
138-
if allow_fill:
139-
msg = "indices are out-of-bounds"
140-
else:
141-
msg = "cannot do a non-empty take from an empty axes"
142-
with pytest.raises(IndexError, match=msg):
143-
cat.take([0], allow_fill=allow_fill)
144-
145-
def test_positional_take(self, ordered):
146-
cat = pd.Categorical(
147-
["a", "a", "b", "b"], categories=["b", "a"], ordered=ordered
148-
)
149-
result = cat.take([0, 1, 2], allow_fill=False)
150-
expected = pd.Categorical(
151-
["a", "a", "b"], categories=cat.categories, ordered=ordered
152-
)
153-
tm.assert_categorical_equal(result, expected)
154-
155-
def test_positional_take_unobserved(self, ordered):
156-
cat = pd.Categorical(["a", "b"], categories=["a", "b", "c"], ordered=ordered)
157-
result = cat.take([1, 0], allow_fill=False)
158-
expected = pd.Categorical(
159-
["b", "a"], categories=cat.categories, ordered=ordered
160-
)
161-
tm.assert_categorical_equal(result, expected)
162-
163-
def test_take_allow_fill(self):
164-
# https://github.com/pandas-dev/pandas/issues/23296
165-
cat = pd.Categorical(["a", "a", "b"])
166-
result = cat.take([0, -1, -1], allow_fill=True)
167-
expected = pd.Categorical(["a", np.nan, np.nan], categories=["a", "b"])
168-
tm.assert_categorical_equal(result, expected)
169-
170-
def test_take_fill_with_negative_one(self):
171-
# -1 was a category
172-
cat = pd.Categorical([-1, 0, 1])
173-
result = cat.take([0, -1, 1], allow_fill=True, fill_value=-1)
174-
expected = pd.Categorical([-1, -1, 0], categories=[-1, 0, 1])
175-
tm.assert_categorical_equal(result, expected)
176-
177-
def test_take_fill_value(self):
178-
# https://github.com/pandas-dev/pandas/issues/23296
179-
cat = pd.Categorical(["a", "b", "c"])
180-
result = cat.take([0, 1, -1], fill_value="a", allow_fill=True)
181-
expected = pd.Categorical(["a", "b", "a"], categories=["a", "b", "c"])
182-
tm.assert_categorical_equal(result, expected)
183-
184-
def test_take_fill_value_new_raises(self):
185-
# https://github.com/pandas-dev/pandas/issues/23296
186-
cat = pd.Categorical(["a", "b", "c"])
187-
xpr = r"'fill_value=d' is not present in this Categorical's categories"
188-
with pytest.raises(ValueError, match=xpr):
189-
cat.take([0, 1, -1], fill_value="d", allow_fill=True)
190-
191-
def test_take_nd_deprecated(self):
192-
cat = pd.Categorical(["a", "b", "c"])
193-
with tm.assert_produces_warning(FutureWarning):
194-
cat.take_nd([0, 1])
195-
196-
ci = pd.Index(cat)
197-
with tm.assert_produces_warning(FutureWarning):
198-
ci.take_nd([0, 1])

pandas/tests/arrays/categorical/test_analytics.py

-7
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,3 @@ def test_validate_inplace_raises(self, value):
359359

360360
with pytest.raises(ValueError, match=msg):
361361
cat.sort_values(inplace=value)
362-
363-
def test_isna(self):
364-
exp = np.array([False, False, True])
365-
c = Categorical(["a", "b", np.nan])
366-
res = c.isna()
367-
368-
tm.assert_numpy_array_equal(res, exp)

pandas/tests/arrays/categorical/test_indexing.py

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import Categorical, CategoricalIndex, Index, PeriodIndex, Series
4+
from pandas import (
5+
Categorical,
6+
CategoricalIndex,
7+
Index,
8+
Interval,
9+
IntervalIndex,
10+
PeriodIndex,
11+
Series,
12+
Timedelta,
13+
Timestamp,
14+
)
515
import pandas._testing as tm
616
import pandas.core.common as com
717
from pandas.tests.arrays.categorical.common import TestCategorical
@@ -256,6 +266,55 @@ def test_where_ordered_differs_rasies(self):
256266
ser.where([True, False, True], other)
257267

258268

269+
class TestContains:
270+
def test_contains(self):
271+
# GH#21508
272+
c = Categorical(list("aabbca"), categories=list("cab"))
273+
274+
assert "b" in c
275+
assert "z" not in c
276+
assert np.nan not in c
277+
with pytest.raises(TypeError, match="unhashable type: 'list'"):
278+
assert [1] in c
279+
280+
# assert codes NOT in index
281+
assert 0 not in c
282+
assert 1 not in c
283+
284+
c = Categorical(list("aabbca") + [np.nan], categories=list("cab"))
285+
assert np.nan in c
286+
287+
@pytest.mark.parametrize(
288+
"item, expected",
289+
[
290+
(Interval(0, 1), True),
291+
(1.5, True),
292+
(Interval(0.5, 1.5), False),
293+
("a", False),
294+
(Timestamp(1), False),
295+
(Timedelta(1), False),
296+
],
297+
ids=str,
298+
)
299+
def test_contains_interval(self, item, expected):
300+
# GH#23705
301+
cat = Categorical(IntervalIndex.from_breaks(range(3)))
302+
result = item in cat
303+
assert result is expected
304+
305+
def test_contains_list(self):
306+
# GH#21729
307+
cat = Categorical([1, 2, 3])
308+
309+
assert "a" not in cat
310+
311+
with pytest.raises(TypeError, match="unhashable type"):
312+
["a"] in cat
313+
314+
with pytest.raises(TypeError, match="unhashable type"):
315+
["a", "b"] in cat
316+
317+
259318
@pytest.mark.parametrize("index", [True, False])
260319
def test_mask_with_boolean(index):
261320
s = Series(range(3))

pandas/tests/arrays/categorical/test_missing.py

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212

1313
class TestCategoricalMissing:
14+
def test_isna(self):
15+
exp = np.array([False, False, True])
16+
cat = Categorical(["a", "b", np.nan])
17+
res = cat.isna()
18+
19+
tm.assert_numpy_array_equal(res, exp)
20+
1421
def test_na_flags_int_categories(self):
1522
# #1457
1623

pandas/tests/arrays/categorical/test_operators.py

-47
Original file line numberDiff line numberDiff line change
@@ -395,50 +395,3 @@ def test_numeric_like_ops(self):
395395
msg = "Object with dtype category cannot perform the numpy op log"
396396
with pytest.raises(TypeError, match=msg):
397397
np.log(s)
398-
399-
def test_contains(self):
400-
# GH21508
401-
c = Categorical(list("aabbca"), categories=list("cab"))
402-
403-
assert "b" in c
404-
assert "z" not in c
405-
assert np.nan not in c
406-
with pytest.raises(TypeError, match="unhashable type: 'list'"):
407-
assert [1] in c
408-
409-
# assert codes NOT in index
410-
assert 0 not in c
411-
assert 1 not in c
412-
413-
c = Categorical(list("aabbca") + [np.nan], categories=list("cab"))
414-
assert np.nan in c
415-
416-
@pytest.mark.parametrize(
417-
"item, expected",
418-
[
419-
(pd.Interval(0, 1), True),
420-
(1.5, True),
421-
(pd.Interval(0.5, 1.5), False),
422-
("a", False),
423-
(pd.Timestamp(1), False),
424-
(pd.Timedelta(1), False),
425-
],
426-
ids=str,
427-
)
428-
def test_contains_interval(self, item, expected):
429-
# GH 23705
430-
cat = Categorical(pd.IntervalIndex.from_breaks(range(3)))
431-
result = item in cat
432-
assert result is expected
433-
434-
def test_contains_list(self):
435-
# GH#21729
436-
cat = Categorical([1, 2, 3])
437-
438-
assert "a" not in cat
439-
440-
with pytest.raises(TypeError, match="unhashable type"):
441-
["a"] in cat
442-
443-
with pytest.raises(TypeError, match="unhashable type"):
444-
["a", "b"] in cat

pandas/tests/arrays/categorical/test_replace.py

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
import pandas as pd
5+
from pandas import Categorical
56
import pandas._testing as tm
67

78

@@ -45,3 +46,28 @@ def test_replace(to_replace, value, expected, flip_categories):
4546

4647
tm.assert_series_equal(expected, result, check_category_order=False)
4748
tm.assert_series_equal(expected, s, check_category_order=False)
49+
50+
51+
@pytest.mark.parametrize(
52+
"to_replace, value, result, expected_error_msg",
53+
[
54+
("b", "c", ["a", "c"], "Categorical.categories are different"),
55+
("c", "d", ["a", "b"], None),
56+
# https://github.com/pandas-dev/pandas/issues/33288
57+
("a", "a", ["a", "b"], None),
58+
("b", None, ["a", None], "Categorical.categories length are different"),
59+
],
60+
)
61+
def test_replace2(to_replace, value, result, expected_error_msg):
62+
# TODO: better name
63+
# GH#26988
64+
cat = Categorical(["a", "b"])
65+
expected = Categorical(result)
66+
result = cat.replace(to_replace, value)
67+
tm.assert_categorical_equal(result, expected)
68+
if to_replace == "b": # the "c" test is supposed to be unchanged
69+
with pytest.raises(AssertionError, match=expected_error_msg):
70+
# ensure non-inplace call does not affect original
71+
tm.assert_categorical_equal(cat, expected)
72+
cat.replace(to_replace, value, inplace=True)
73+
tm.assert_categorical_equal(cat, expected)

0 commit comments

Comments
 (0)