MCPcopy
hub / github.com/pandas-dev/pandas / test_categorical

Function test_categorical

pandas/tests/io/pytables/test_categorical.py:16–123  ·  view source on GitHub ↗
(temp_hdfstore)

Source from the content-addressed store, hash-verified

14
15
16def test_categorical(temp_hdfstore):
17 # Basic
18 s = Series(
19 Categorical(
20 ["a", "b", "b", "a", "a", "c"],
21 categories=["a", "b", "c", "d"],
22 ordered=False,
23 )
24 )
25 temp_hdfstore.append("s", s, format="table")
26 result = temp_hdfstore.select("s")
27 tm.assert_series_equal(s, result)
28
29 s = Series(
30 Categorical(
31 ["a", "b", "b", "a", "a", "c"],
32 categories=["a", "b", "c", "d"],
33 ordered=True,
34 )
35 )
36 temp_hdfstore.append("s_ordered", s, format="table")
37 result = temp_hdfstore.select("s_ordered")
38 tm.assert_series_equal(s, result)
39
40 df = DataFrame({"s": s, "vals": [1, 2, 3, 4, 5, 6]})
41 temp_hdfstore.append("df", df, format="table")
42 result = temp_hdfstore.select("df")
43 tm.assert_frame_equal(result, df)
44
45 # Dtypes
46 s = Series([1, 1, 2, 2, 3, 4, 5]).astype("category")
47 temp_hdfstore.append("si", s)
48 result = temp_hdfstore.select("si")
49 tm.assert_series_equal(result, s)
50
51 s = Series([1, 1, np.nan, 2, 3, 4, 5]).astype("category")
52 temp_hdfstore.append("si2", s)
53 result = temp_hdfstore.select("si2")
54 tm.assert_series_equal(result, s)
55
56 # Multiple
57 df2 = df.copy()
58 df2["s2"] = Series(list("abcdefg")).astype("category")
59 temp_hdfstore.append("df2", df2)
60 result = temp_hdfstore.select("df2")
61 tm.assert_frame_equal(result, df2)
62
63 # Make sure the metadata is OK
64 info = temp_hdfstore.info()
65 assert "/df2 " in info
66 # df2._mgr.blocks[0] and df2._mgr.blocks[2] are Categorical
67 assert "/df2/meta/values_block_0/meta" in info
68 assert "/df2/meta/values_block_2/meta" in info
69
70 # unordered
71 s = Series(
72 Categorical(
73 ["a", "b", "b", "a", "a", "c"],

Callers

nothing calls this directly

Calls 13

SeriesClass · 0.90
CategoricalClass · 0.90
DataFrameClass · 0.90
concatFunction · 0.90
appendMethod · 0.45
selectMethod · 0.45
astypeMethod · 0.45
copyMethod · 0.45
infoMethod · 0.45
isinMethod · 0.45
raisesMethod · 0.45

Tested by

no test coverage detected