Set categories to be unknown. Parameters ---------- x : DataFrame, Series, Index cols : iterable, optional If x is a DataFrame, set only categoricals in these columns to unknown. By default, all categorical columns are set to unknown categoricals index : bool, op
(x, cols=None, index=True, dtype_backend=None)
| 218 | |
| 219 | |
| 220 | def clear_known_categories(x, cols=None, index=True, dtype_backend=None): |
| 221 | """Set categories to be unknown. |
| 222 | |
| 223 | Parameters |
| 224 | ---------- |
| 225 | x : DataFrame, Series, Index |
| 226 | cols : iterable, optional |
| 227 | If x is a DataFrame, set only categoricals in these columns to unknown. |
| 228 | By default, all categorical columns are set to unknown categoricals |
| 229 | index : bool, optional |
| 230 | If True and x is a Series or DataFrame, set the clear known categories |
| 231 | in the index as well. |
| 232 | dtype_backend : string, optional |
| 233 | If set to PyArrow, the categorical dtype is implemented as a PyArrow |
| 234 | dictionary |
| 235 | """ |
| 236 | if dtype_backend == "pyarrow": |
| 237 | # Right now Categorical with PyArrow is implemented as dictionary and |
| 238 | # categorical accessor is not yet available |
| 239 | return x |
| 240 | |
| 241 | if not is_index_like(x): |
| 242 | x = x.copy() |
| 243 | if is_dataframe_like(x): |
| 244 | mask = x.dtypes == "category" |
| 245 | if cols is None: |
| 246 | cols = mask[mask].index |
| 247 | elif not mask.loc[cols].all(): |
| 248 | raise ValueError("Not all columns are categoricals") |
| 249 | for c in cols: |
| 250 | x[c] = x[c].cat.set_categories([UNKNOWN_CATEGORIES]) |
| 251 | elif is_series_like(x): |
| 252 | if is_categorical_dtype_dispatch(x.dtype): |
| 253 | x = x.cat.set_categories([UNKNOWN_CATEGORIES]) |
| 254 | if index and is_categorical_dtype_dispatch(x.index.dtype): |
| 255 | x.index = x.index.set_categories([UNKNOWN_CATEGORIES]) |
| 256 | elif is_categorical_dtype_dispatch(x.dtype): |
| 257 | x = x.set_categories([UNKNOWN_CATEGORIES]) |
| 258 | return x |
| 259 | |
| 260 | |
| 261 | def _empty_series(name, dtype, index=None): |
no test coverage detected
searching dependent graphs…