|
44 | 44 | from pandas.core.accessor import PandasDelegate, delegate_names
|
45 | 45 | import pandas.core.algorithms as algorithms
|
46 | 46 | from pandas.core.algorithms import _get_data_algo, factorize, take_1d, unique1d
|
47 |
| -from pandas.core.array_algos.transforms import shift |
48 |
| -from pandas.core.arrays._mixins import _T, NDArrayBackedExtensionArray |
| 47 | +from pandas.core.arrays._mixins import NDArrayBackedExtensionArray |
49 | 48 | from pandas.core.base import (
|
50 | 49 | ExtensionArray,
|
51 | 50 | NoNewAttributesMixin,
|
@@ -1193,35 +1192,6 @@ def map(self, mapper):
|
1193 | 1192 | __le__ = _cat_compare_op(operator.le)
|
1194 | 1193 | __ge__ = _cat_compare_op(operator.ge)
|
1195 | 1194 |
|
1196 |
| - def shift(self, periods, fill_value=None): |
1197 |
| - """ |
1198 |
| - Shift Categorical by desired number of periods. |
1199 |
| -
|
1200 |
| - Parameters |
1201 |
| - ---------- |
1202 |
| - periods : int |
1203 |
| - Number of periods to move, can be positive or negative |
1204 |
| - fill_value : object, optional |
1205 |
| - The scalar value to use for newly introduced missing values. |
1206 |
| -
|
1207 |
| - .. versionadded:: 0.24.0 |
1208 |
| -
|
1209 |
| - Returns |
1210 |
| - ------- |
1211 |
| - shifted : Categorical |
1212 |
| - """ |
1213 |
| - # since categoricals always have ndim == 1, an axis parameter |
1214 |
| - # doesn't make any sense here. |
1215 |
| - codes = self.codes |
1216 |
| - if codes.ndim > 1: |
1217 |
| - raise NotImplementedError("Categorical with ndim > 1.") |
1218 |
| - |
1219 |
| - fill_value = self._validate_fill_value(fill_value) |
1220 |
| - |
1221 |
| - codes = shift(codes, periods, axis=0, fill_value=fill_value) |
1222 |
| - |
1223 |
| - return self._constructor(codes, dtype=self.dtype, fastpath=True) |
1224 |
| - |
1225 | 1195 | def _validate_fill_value(self, fill_value):
|
1226 | 1196 | """
|
1227 | 1197 | Convert a user-facing fill_value to a representation to use with our
|
@@ -1383,20 +1353,6 @@ def notna(self):
|
1383 | 1353 |
|
1384 | 1354 | notnull = notna
|
1385 | 1355 |
|
1386 |
| - def dropna(self): |
1387 |
| - """ |
1388 |
| - Return the Categorical without null values. |
1389 |
| -
|
1390 |
| - Missing values (-1 in .codes) are detected. |
1391 |
| -
|
1392 |
| - Returns |
1393 |
| - ------- |
1394 |
| - valid : Categorical |
1395 |
| - """ |
1396 |
| - result = self[self.notna()] |
1397 |
| - |
1398 |
| - return result |
1399 |
| - |
1400 | 1356 | def value_counts(self, dropna=True):
|
1401 | 1357 | """
|
1402 | 1358 | Return a Series containing counts of each category.
|
@@ -1749,81 +1705,6 @@ def fillna(self, value=None, method=None, limit=None):
|
1749 | 1705 |
|
1750 | 1706 | return self._constructor(codes, dtype=self.dtype, fastpath=True)
|
1751 | 1707 |
|
1752 |
| - def take(self: _T, indexer, allow_fill: bool = False, fill_value=None) -> _T: |
1753 |
| - """ |
1754 |
| - Take elements from the Categorical. |
1755 |
| -
|
1756 |
| - Parameters |
1757 |
| - ---------- |
1758 |
| - indexer : sequence of int |
1759 |
| - The indices in `self` to take. The meaning of negative values in |
1760 |
| - `indexer` depends on the value of `allow_fill`. |
1761 |
| - allow_fill : bool, default False |
1762 |
| - How to handle negative values in `indexer`. |
1763 |
| -
|
1764 |
| - * False: negative values in `indices` indicate positional indices |
1765 |
| - from the right. This is similar to |
1766 |
| - :func:`numpy.take`. |
1767 |
| -
|
1768 |
| - * True: negative values in `indices` indicate missing values |
1769 |
| - (the default). These values are set to `fill_value`. Any other |
1770 |
| - other negative values raise a ``ValueError``. |
1771 |
| -
|
1772 |
| - .. versionchanged:: 1.0.0 |
1773 |
| -
|
1774 |
| - Default value changed from ``True`` to ``False``. |
1775 |
| -
|
1776 |
| - fill_value : object |
1777 |
| - The value to use for `indices` that are missing (-1), when |
1778 |
| - ``allow_fill=True``. This should be the category, i.e. a value |
1779 |
| - in ``self.categories``, not a code. |
1780 |
| -
|
1781 |
| - Returns |
1782 |
| - ------- |
1783 |
| - Categorical |
1784 |
| - This Categorical will have the same categories and ordered as |
1785 |
| - `self`. |
1786 |
| -
|
1787 |
| - See Also |
1788 |
| - -------- |
1789 |
| - Series.take : Similar method for Series. |
1790 |
| - numpy.ndarray.take : Similar method for NumPy arrays. |
1791 |
| -
|
1792 |
| - Examples |
1793 |
| - -------- |
1794 |
| - >>> cat = pd.Categorical(['a', 'a', 'b']) |
1795 |
| - >>> cat |
1796 |
| - ['a', 'a', 'b'] |
1797 |
| - Categories (2, object): ['a', 'b'] |
1798 |
| -
|
1799 |
| - Specify ``allow_fill==False`` to have negative indices mean indexing |
1800 |
| - from the right. |
1801 |
| -
|
1802 |
| - >>> cat.take([0, -1, -2], allow_fill=False) |
1803 |
| - ['a', 'b', 'a'] |
1804 |
| - Categories (2, object): ['a', 'b'] |
1805 |
| -
|
1806 |
| - With ``allow_fill=True``, indices equal to ``-1`` mean "missing" |
1807 |
| - values that should be filled with the `fill_value`, which is |
1808 |
| - ``np.nan`` by default. |
1809 |
| -
|
1810 |
| - >>> cat.take([0, -1, -1], allow_fill=True) |
1811 |
| - ['a', NaN, NaN] |
1812 |
| - Categories (2, object): ['a', 'b'] |
1813 |
| -
|
1814 |
| - The fill value can be specified. |
1815 |
| -
|
1816 |
| - >>> cat.take([0, -1, -1], allow_fill=True, fill_value='a') |
1817 |
| - ['a', 'a', 'a'] |
1818 |
| - Categories (2, object): ['a', 'b'] |
1819 |
| -
|
1820 |
| - Specifying a fill value that's not in ``self.categories`` |
1821 |
| - will raise a ``ValueError``. |
1822 |
| - """ |
1823 |
| - return NDArrayBackedExtensionArray.take( |
1824 |
| - self, indexer, allow_fill=allow_fill, fill_value=fill_value |
1825 |
| - ) |
1826 |
| - |
1827 | 1708 | # ------------------------------------------------------------------
|
1828 | 1709 | # NDArrayBackedExtensionArray compat
|
1829 | 1710 |
|
@@ -1861,6 +1742,9 @@ def __contains__(self, key) -> bool:
|
1861 | 1742 |
|
1862 | 1743 | return contains(self, key, container=self._codes)
|
1863 | 1744 |
|
| 1745 | + # ------------------------------------------------------------------ |
| 1746 | + # Rendering Methods |
| 1747 | + |
1864 | 1748 | def _tidy_repr(self, max_vals=10, footer=True) -> str:
|
1865 | 1749 | """
|
1866 | 1750 | a short repr displaying only max_vals and an optional (but default
|
@@ -1959,6 +1843,8 @@ def __repr__(self) -> str:
|
1959 | 1843 |
|
1960 | 1844 | return result
|
1961 | 1845 |
|
| 1846 | + # ------------------------------------------------------------------ |
| 1847 | + |
1962 | 1848 | def _maybe_coerce_indexer(self, indexer):
|
1963 | 1849 | """
|
1964 | 1850 | return an indexer coerced to the codes dtype
|
|
0 commit comments