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

Function test_str_cat_categorical

pandas/tests/strings/test_cat.py:100–152  ·  view source on GitHub ↗
(
    index_or_series, dtype_caller, dtype_target, sep, infer_string
)

Source from the content-addressed store, hash-verified

98@pytest.mark.parametrize("dtype_target", ["object", "category"])
99@pytest.mark.parametrize("dtype_caller", ["object", "category"])
100def test_str_cat_categorical(
101 index_or_series, dtype_caller, dtype_target, sep, infer_string
102):
103 box = index_or_series
104
105 with option_context("future.infer_string", infer_string):
106 s = Index(["a", "a", "b", "a"], dtype=dtype_caller)
107 s = s if box == Index else Series(s, index=s, dtype=s.dtype)
108 t = Index(["b", "a", "b", "c"], dtype=dtype_target)
109
110 expected = Index(
111 ["ab", "aa", "bb", "ac"], dtype=object if dtype_caller == "object" else None
112 )
113 expected = (
114 expected
115 if box == Index
116 else Series(
117 expected, index=Index(s, dtype=dtype_caller), dtype=expected.dtype
118 )
119 )
120
121 # Series/Index with unaligned Index -> t.values
122 result = s.str.cat(t.values, sep=sep)
123 tm.assert_equal(result, expected)
124
125 # Series/Index with Series having matching Index
126 t = Series(t.values, index=Index(s, dtype=dtype_caller))
127 result = s.str.cat(t, sep=sep)
128 tm.assert_equal(result, expected)
129
130 # Series/Index with Series.values
131 result = s.str.cat(t.values, sep=sep)
132 tm.assert_equal(result, expected)
133
134 # Series/Index with Series having different Index
135 t = Series(t.values, index=t.values)
136 expected = Index(
137 ["aa", "aa", "bb", "bb", "aa"],
138 dtype=object if dtype_caller == "object" else None,
139 )
140 dtype = object if dtype_caller == "object" else s.dtype.categories.dtype
141 expected = (
142 expected
143 if box == Index
144 else Series(
145 expected,
146 index=Index(expected.str[:1], dtype=dtype),
147 dtype=expected.dtype,
148 )
149 )
150
151 result = s.str.cat(t, sep=sep)
152 tm.assert_equal(result, expected)
153
154
155@pytest.mark.parametrize(

Callers

nothing calls this directly

Calls 4

option_contextFunction · 0.90
IndexClass · 0.90
SeriesClass · 0.90
catMethod · 0.80

Tested by

no test coverage detected