Skip to content

Commit 3255910

Browse files
author
adrien pacifico
committed
- update tests for categorical values
1 parent 1e3037d commit 3255910

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

pandas/tests/reshape/concat/test_categorical.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pandas as pd
88
from pandas import (
99
Categorical,
10+
CategoricalIndex,
1011
DataFrame,
1112
Series,
1213
)
@@ -75,13 +76,13 @@ def test_concat_categoricalindex(self):
7576
# GH 16111, categories that aren't lexsorted
7677
categories = [9, 0, 1, 2, 3]
7778

78-
a = Series(1, index=pd.CategoricalIndex([9, 0], categories=categories))
79-
b = Series(2, index=pd.CategoricalIndex([0, 1], categories=categories))
80-
c = Series(3, index=pd.CategoricalIndex([1, 2], categories=categories))
79+
a = Series(1, index=CategoricalIndex([9, 0], categories=categories))
80+
b = Series(2, index=CategoricalIndex([0, 1], categories=categories))
81+
c = Series(3, index=CategoricalIndex([1, 2], categories=categories))
8182

8283
result = pd.concat([a, b, c], axis=1)
8384

84-
exp_idx = pd.CategoricalIndex([9, 0, 1, 2], categories=categories)
85+
exp_idx = CategoricalIndex([9, 0, 1, 2], categories=categories)
8586
exp = DataFrame(
8687
{
8788
0: [1, 1, np.nan, np.nan],
@@ -99,7 +100,7 @@ def test_categorical_concat_preserve(self):
99100
s = Series(list("abc"), dtype="category")
100101
s2 = Series(list("abd"), dtype="category")
101102

102-
exp = Series(list("abcabd"))
103+
exp = Series(list("abcabd"), dtype="category")
103104
res = pd.concat([s, s2], ignore_index=True)
104105
tm.assert_series_equal(res, exp)
105106

@@ -147,8 +148,8 @@ def test_categorical_index_preserver(self):
147148
result = pd.concat([df2, df3])
148149
expected = pd.concat(
149150
[
150-
df2.set_axis(df2.index.astype(object), axis=0),
151-
df3.set_axis(df3.index.astype(object), axis=0),
151+
df2.set_axis(df2.index.astype("category"), axis=0),
152+
df3.set_axis(df3.index.astype("category"), axis=0),
152153
]
153154
)
154155
tm.assert_frame_equal(result, expected)
@@ -179,7 +180,8 @@ def test_concat_categorical_datetime(self):
179180

180181
result = pd.concat([df1, df2])
181182
expected = DataFrame(
182-
{"x": Series([datetime(2021, 1, 1), datetime(2021, 1, 2)])}
183+
{"x": Series([datetime(2021, 1, 1), datetime(2021, 1, 2)])},
184+
dtype="category",
183185
)
184186

185187
tm.assert_equal(result, expected)
@@ -227,15 +229,17 @@ def test_categorical_index_upcast(self):
227229
b = DataFrame({"foo": [4, 3]}, index=Categorical(["baz", "bar"]))
228230

229231
res = pd.concat([a, b])
230-
exp = DataFrame({"foo": [1, 2, 4, 3]}, index=["foo", "bar", "baz", "bar"])
232+
exp = DataFrame(
233+
{"foo": [1, 2, 4, 3]}, index=Categorical(["foo", "bar", "baz", "bar"])
234+
)
231235

232236
tm.assert_equal(res, exp)
233237

234238
a = Series([1, 2], index=Categorical(["foo", "bar"]))
235239
b = Series([4, 3], index=Categorical(["baz", "bar"]))
236240

237241
res = pd.concat([a, b])
238-
exp = Series([1, 2, 4, 3], index=["foo", "bar", "baz", "bar"])
242+
exp = Series([1, 2, 4, 3], index=Categorical(["foo", "bar", "baz", "bar"]))
239243

240244
tm.assert_equal(res, exp)
241245

@@ -257,9 +261,9 @@ def test_categorical_missing_from_one_frame(self):
257261
def test_concat_categorical_same_categories_different_order(self):
258262
# https://github.com/pandas-dev/pandas/issues/24845
259263

260-
c1 = pd.CategoricalIndex(["a", "a"], categories=["a", "b"], ordered=False)
261-
c2 = pd.CategoricalIndex(["b", "b"], categories=["b", "a"], ordered=False)
262-
c3 = pd.CategoricalIndex(
264+
c1 = CategoricalIndex(["a", "a"], categories=["a", "b"], ordered=False)
265+
c2 = CategoricalIndex(["b", "b"], categories=["b", "a"], ordered=False)
266+
c3 = CategoricalIndex(
263267
["a", "a", "b", "b"], categories=["a", "b"], ordered=False
264268
)
265269

0 commit comments

Comments
 (0)