|
8 | 8 | from pandas.compat import u |
9 | 9 |
|
10 | 10 | from pandas.core.algorithms import factorize |
11 | | -from pandas.core.base import PandasObject, PandasDelegate, NoNewAttributesMixin |
| 11 | +from pandas.core.base import (PandasObject, PandasDelegate, |
| 12 | + NoNewAttributesMixin, _shared_docs) |
12 | 13 | import pandas.core.common as com |
13 | 14 | from pandas.core.missing import interpolate_2d |
14 | | -from pandas.util.decorators import cache_readonly, deprecate_kwarg |
| 15 | +from pandas.util.decorators import (Appender, cache_readonly, |
| 16 | + deprecate_kwarg, Substitution) |
15 | 17 |
|
16 | 18 | from pandas.core.common import ( |
17 | 19 | ABCSeries, ABCIndexClass, ABCCategoricalIndex, isnull, notnull, |
@@ -1003,67 +1005,18 @@ def memory_usage(self, deep=False): |
1003 | 1005 | """ |
1004 | 1006 | return self._codes.nbytes + self._categories.memory_usage(deep=deep) |
1005 | 1007 |
|
| 1008 | + @Substitution(klass='Categorical', value='v') |
| 1009 | + @Appender(_shared_docs['searchsorted']) |
1006 | 1010 | def searchsorted(self, v, side='left', sorter=None): |
1007 | | - """Find indices where elements should be inserted to maintain order. |
1008 | | -
|
1009 | | - Find the indices into a sorted Categorical `self` such that, if the |
1010 | | - corresponding elements in `v` were inserted before the indices, the |
1011 | | - order of `self` would be preserved. |
1012 | | -
|
1013 | | - Parameters |
1014 | | - ---------- |
1015 | | - v : array_like |
1016 | | - Array-like values or a scalar value, to insert/search for in |
1017 | | - `self`. |
1018 | | - side : {'left', 'right'}, optional |
1019 | | - If 'left', the index of the first suitable location found is given. |
1020 | | - If 'right', return the last such index. If there is no suitable |
1021 | | - index, return either 0 or N (where N is the length of `a`). |
1022 | | - sorter : 1-D array_like, optional |
1023 | | - Optional array of integer indices that sort `self` into ascending |
1024 | | - order. They are typically the result of ``np.argsort``. |
1025 | | -
|
1026 | | - Returns |
1027 | | - ------- |
1028 | | - indices : array of ints |
1029 | | - Array of insertion points with the same shape as `v`. |
1030 | | -
|
1031 | | - See Also |
1032 | | - -------- |
1033 | | - Series.searchsorted |
1034 | | - numpy.searchsorted |
1035 | | -
|
1036 | | - Notes |
1037 | | - ----- |
1038 | | - Binary search is used to find the required insertion points. |
1039 | | -
|
1040 | | - Examples |
1041 | | - -------- |
1042 | | - >>> x = pd.Categorical(['apple', 'bread', 'bread', 'cheese', 'milk' ]) |
1043 | | - [apple, bread, bread, cheese, milk] |
1044 | | - Categories (4, object): [apple < bread < cheese < milk] |
1045 | | - >>> x.searchsorted('bread') |
1046 | | - array([1]) # Note: an array, not a scalar |
1047 | | - >>> x.searchsorted(['bread']) |
1048 | | - array([1]) |
1049 | | - >>> x.searchsorted(['bread', 'eggs']) |
1050 | | - array([1, 4]) |
1051 | | - >>> x.searchsorted(['bread', 'eggs'], side='right') |
1052 | | - array([3, 4]) # eggs before milk |
1053 | | - >>> x = pd.Categorical(['apple', 'bread', 'bread', 'cheese', 'milk', |
1054 | | - 'donuts' ]) |
1055 | | - >>> x.searchsorted(['bread', 'eggs'], side='right', |
1056 | | - sorter=[0, 1, 2, 3, 5, 4]) |
1057 | | - array([3, 5]) # eggs after donuts, after switching milk and donuts |
1058 | | - """ |
1059 | 1011 | if not self.ordered: |
1060 | 1012 | raise ValueError("Categorical not ordered\nyou can use " |
1061 | 1013 | ".as_ordered() to change the Categorical to an " |
1062 | 1014 | "ordered one") |
1063 | 1015 |
|
1064 | 1016 | from pandas.core.series import Series |
1065 | 1017 | values_as_codes = self.categories.values.searchsorted( |
1066 | | - Series(v).values, side) |
| 1018 | + Series(v).values, side=side) |
| 1019 | + |
1067 | 1020 | return self.codes.searchsorted(values_as_codes, sorter=sorter) |
1068 | 1021 |
|
1069 | 1022 | def isnull(self): |
|
0 commit comments