|
10 | 10 | cast, |
11 | 11 | overload, |
12 | 12 | ) |
13 | | -import warnings |
14 | 13 |
|
15 | 14 | import numpy as np |
16 | 15 |
|
|
23 | 22 | ) |
24 | 23 | from pandas._libs.arrays import NDArrayBacked |
25 | 24 | from pandas.compat.numpy import function as nv |
26 | | -from pandas.util._exceptions import find_stack_level |
27 | 25 | from pandas.util._validators import validate_bool_kwarg |
28 | 26 |
|
29 | 27 | from pandas.core.dtypes.cast import ( |
@@ -2673,62 +2671,6 @@ def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]: |
2673 | 2671 | code_values = code_values[null_mask | (code_values >= 0)] |
2674 | 2672 | return algorithms.isin(self.codes, code_values) |
2675 | 2673 |
|
2676 | | - @overload |
2677 | | - def _replace(self, *, to_replace, value, inplace: Literal[False] = ...) -> Self: ... |
2678 | | - |
2679 | | - @overload |
2680 | | - def _replace(self, *, to_replace, value, inplace: Literal[True]) -> None: ... |
2681 | | - |
2682 | | - def _replace(self, *, to_replace, value, inplace: bool = False) -> Self | None: |
2683 | | - from pandas import Index |
2684 | | - |
2685 | | - orig_dtype = self.dtype |
2686 | | - |
2687 | | - inplace = validate_bool_kwarg(inplace, "inplace") |
2688 | | - cat = self if inplace else self.copy() |
2689 | | - |
2690 | | - mask = isna(np.asarray(value)) |
2691 | | - if mask.any(): |
2692 | | - removals = np.asarray(to_replace)[mask] |
2693 | | - removals = cat.categories[cat.categories.isin(removals)] |
2694 | | - new_cat = cat.remove_categories(removals) |
2695 | | - NDArrayBacked.__init__(cat, new_cat.codes, new_cat.dtype) |
2696 | | - |
2697 | | - ser = cat.categories.to_series() |
2698 | | - ser = ser.replace(to_replace=to_replace, value=value) |
2699 | | - |
2700 | | - all_values = Index(ser) |
2701 | | - |
2702 | | - # GH51016: maintain order of existing categories |
2703 | | - idxr = cat.categories.get_indexer_for(all_values) |
2704 | | - locs = np.arange(len(ser)) |
2705 | | - locs = np.where(idxr == -1, locs, idxr) |
2706 | | - locs = locs.argsort() |
2707 | | - |
2708 | | - new_categories = ser.take(locs) |
2709 | | - new_categories = new_categories.drop_duplicates(keep="first") |
2710 | | - index_categories = Index(new_categories) |
2711 | | - new_codes = recode_for_categories( |
2712 | | - cat._codes, all_values, index_categories, copy=False |
2713 | | - ) |
2714 | | - new_dtype = CategoricalDtype(index_categories, ordered=self.dtype.ordered) |
2715 | | - NDArrayBacked.__init__(cat, new_codes, new_dtype) |
2716 | | - |
2717 | | - if new_dtype != orig_dtype: |
2718 | | - warnings.warn( |
2719 | | - # GH#55147 |
2720 | | - "The behavior of Series.replace (and DataFrame.replace) with " |
2721 | | - "CategoricalDtype is deprecated. In a future version, replace " |
2722 | | - "will only be used for cases that preserve the categories. " |
2723 | | - "To change the categories, use ser.cat.rename_categories " |
2724 | | - "instead.", |
2725 | | - FutureWarning, |
2726 | | - stacklevel=find_stack_level(), |
2727 | | - ) |
2728 | | - if not inplace: |
2729 | | - return cat |
2730 | | - return None |
2731 | | - |
2732 | 2674 | # ------------------------------------------------------------------------ |
2733 | 2675 | # String methods interface |
2734 | 2676 | def _str_map( |
|
0 commit comments