From 4237ce7774df822d50a26f022eccffb29ac75885 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 31 Mar 2021 15:36:09 -0700 Subject: [PATCH] CLN: follow-up cleanups --- pandas/_libs/lib.pyx | 3 +++ pandas/core/base.py | 2 +- pandas/core/indexes/base.py | 8 ++------ pandas/core/internals/blocks.py | 22 ++++++++-------------- pandas/core/internals/managers.py | 12 +++--------- pandas/core/series.py | 4 ++-- 6 files changed, 19 insertions(+), 32 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 646b5a05afcad..809aa40f1656c 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -2440,6 +2440,9 @@ class NoDefault(Enum): # 2) because mypy does not understand singletons no_default = "NO_DEFAULT" + def __repr__(self) -> str: + return "" + # Note: no_default is exported to the public API in pandas.api.extensions no_default = NoDefault.no_default # Sentinel indicating the default value. diff --git a/pandas/core/base.py b/pandas/core/base.py index 18fc76fe79a5a..b0c2af89ad0c7 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1164,7 +1164,7 @@ def is_monotonic_decreasing(self) -> bool: return Index(self).is_monotonic_decreasing - def memory_usage(self, deep=False): + def _memory_usage(self, deep: bool = False) -> int: """ Memory usage of the values. diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index e85d09a479d16..771a357963cb7 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -12,7 +12,6 @@ FrozenSet, Hashable, List, - NewType, Optional, Sequence, Set, @@ -195,9 +194,6 @@ _o_dtype = np.dtype("object") -_Identity = NewType("_Identity", object) - - def disallow_kwargs(kwargs: Dict[str, Any]): if kwargs: raise TypeError(f"Unexpected keyword arguments {repr(set(kwargs))}") @@ -4405,9 +4401,9 @@ def _get_engine_target(self) -> np.ndarray: # ndarray]", expected "ndarray") return self._values # type: ignore[return-value] - @doc(IndexOpsMixin.memory_usage) + @doc(IndexOpsMixin._memory_usage) def memory_usage(self, deep: bool = False) -> int: - result = super().memory_usage(deep=deep) + result = self._memory_usage(deep=deep) # include our engine hashtable result += self._engine.sizeof(deep=deep) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a77ea61d9e6de..cdd0fdc85abb9 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -5,7 +5,6 @@ from typing import ( TYPE_CHECKING, Any, - Callable, List, Optional, Tuple, @@ -210,6 +209,7 @@ def is_bool(self) -> bool: def external_values(self): return external_values(self.values) + @final def internal_values(self): """ The array that Series._values returns (internal values). @@ -596,8 +596,6 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"): Block """ values = self.values - if values.dtype.kind in ["m", "M"]: - values = self.array_values new_values = astype_array_safe(values, dtype, copy=copy, errors=errors) @@ -1751,14 +1749,13 @@ class HybridMixin: Mixin for Blocks backed (maybe indirectly) by ExtensionArrays. """ - array_values: Callable + values: NDArrayBackedExtensionArray | IntervalArray def _can_hold_element(self, element: Any) -> bool: - values = self.array_values + values = self.values try: - # error: "Callable[..., Any]" has no attribute "_validate_setitem_value" - values._validate_setitem_value(element) # type: ignore[attr-defined] + values._validate_setitem_value(element) return True except (ValueError, TypeError): return False @@ -1772,6 +1769,8 @@ class ObjectValuesExtensionBlock(HybridMixin, ExtensionBlock): Series[T].values is an ndarray of objects. """ + values: IntervalArray | PeriodArray + pass @@ -1800,8 +1799,8 @@ def is_view(self) -> bool: # check the ndarray values of the DatetimeIndex values return self.values._ndarray.base is not None - def internal_values(self): - # Override to return DatetimeArray and TimedeltaArray + @property + def array_values(self): return self.values def get_values(self, dtype: Optional[DtypeObj] = None) -> np.ndarray: @@ -1901,10 +1900,6 @@ class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock): is_numeric = False - @cache_readonly - def array_values(self): - return self.values - class DatetimeBlock(DatetimeLikeBlockMixin): __slots__ = () @@ -1919,7 +1914,6 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeLikeBlockMixin): is_extension = True is_numeric = False - internal_values = Block.internal_values _can_hold_element = DatetimeBlock._can_hold_element diff = DatetimeBlock.diff where = DatetimeBlock.where diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 6681015856d6b..5f79011668123 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1086,17 +1086,11 @@ def value_getitem(placement): else: if value.ndim == 2: value = value.T - - if value.ndim == self.ndim - 1: - value = ensure_block_shape(value, ndim=2) - - def value_getitem(placement): - return value - else: + value = ensure_block_shape(value, ndim=2) - def value_getitem(placement): - return value[placement.indexer] + def value_getitem(placement): + return value[placement.indexer] if value.shape[1:] != self.shape[1:]: raise AssertionError( diff --git a/pandas/core/series.py b/pandas/core/series.py index 4b89c09cdb898..1370962dd9d29 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4609,7 +4609,7 @@ def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> Series: periods=periods, freq=freq, axis=axis, fill_value=fill_value ) - def memory_usage(self, index=True, deep=False): + def memory_usage(self, index: bool = True, deep: bool = False) -> int: """ Return the memory usage of the Series. @@ -4658,7 +4658,7 @@ def memory_usage(self, index=True, deep=False): >>> s.memory_usage(deep=True) 244 """ - v = super().memory_usage(deep=deep) + v = self._memory_usage(deep=deep) if index: v += self.index.memory_usage(deep=deep) return v