| 118 | |
| 119 | @pytest.mark.parametrize("dtype", [object, "category"]) |
| 120 | def test_api_per_method( |
| 121 | index_or_series, |
| 122 | dtype, |
| 123 | any_allowed_skipna_inferred_dtype, |
| 124 | any_string_method, |
| 125 | request, |
| 126 | using_infer_string, |
| 127 | ): |
| 128 | # this test does not check correctness of the different methods, |
| 129 | # just that the methods work on the specified (inferred) dtypes, |
| 130 | # and raise on all others |
| 131 | box = index_or_series |
| 132 | |
| 133 | # one instance of each parametrized fixture |
| 134 | inferred_dtype, values = any_allowed_skipna_inferred_dtype |
| 135 | method_name, args, kwargs = any_string_method |
| 136 | |
| 137 | reason = None |
| 138 | if box is Index and values.size == 0: |
| 139 | if method_name in ["partition", "rpartition"] and kwargs.get("expand", True): |
| 140 | raises = TypeError |
| 141 | reason = "Method cannot deal with empty Index" |
| 142 | elif method_name == "split" and kwargs.get("expand", None): |
| 143 | raises = TypeError |
| 144 | reason = "Split fails on empty Series when expand=True" |
| 145 | elif method_name == "get_dummies": |
| 146 | raises = ValueError |
| 147 | reason = "Need to fortify get_dummies corner cases" |
| 148 | |
| 149 | elif ( |
| 150 | box is Index |
| 151 | and inferred_dtype == "empty" |
| 152 | and dtype == object |
| 153 | and method_name == "get_dummies" |
| 154 | ): |
| 155 | raises = ValueError |
| 156 | reason = "Need to fortify get_dummies corner cases" |
| 157 | |
| 158 | if reason is not None: |
| 159 | mark = pytest.mark.xfail(raises=raises, reason=reason) |
| 160 | request.applymarker(mark) |
| 161 | |
| 162 | t = box(values, dtype=dtype) # explicit dtype to avoid casting |
| 163 | method = getattr(t.str, method_name) |
| 164 | |
| 165 | if using_infer_string and dtype == "category": |
| 166 | string_allowed = method_name not in ["decode"] |
| 167 | else: |
| 168 | string_allowed = True |
| 169 | bytes_allowed = method_name in ["decode", "get", "len", "slice"] |
| 170 | # as of v0.23.4, all methods except 'cat' are very lenient with the |
| 171 | # allowed data types, just returning NaN for entries that error. |
| 172 | # This could be changed with an 'errors'-kwarg to the `str`-accessor, |
| 173 | # see discussion in GH 13877 |
| 174 | mixed_allowed = method_name not in ["cat"] |
| 175 | |
| 176 | allowed_types = ( |
| 177 | ["empty"] |