From 61bb5186ae40a1e7271e9ce6c53f78785b788fb2 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 30 Jul 2021 13:56:58 -0700 Subject: [PATCH] CLN: collected cleanups --- pandas/core/base.py | 4 ++-- pandas/core/construction.py | 2 +- pandas/core/indexes/datetimelike.py | 3 ++- pandas/core/indexing.py | 24 +++++++++++------------- pandas/core/internals/concat.py | 1 + pandas/core/series.py | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 4d380c6831071..7d51b50f783a5 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1221,7 +1221,7 @@ def factorize(self, sort: bool = False, na_sentinel: int | None = -1): """ @doc(_shared_docs["searchsorted"], klass="Index") - def searchsorted(self, value, side="left", sorter=None) -> np.ndarray: + def searchsorted(self, value, side="left", sorter=None) -> npt.NDArray[np.intp]: return algorithms.searchsorted(self._values, value, side=side, sorter=sorter) def drop_duplicates(self, keep="first"): @@ -1232,5 +1232,5 @@ def drop_duplicates(self, keep="first"): @final def _duplicated( self, keep: Literal["first", "last", False] = "first" - ) -> np.ndarray: + ) -> npt.NDArray[np.bool_]: return duplicated(self._values, keep=keep) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 89591f27e9092..f84aaa907f3fc 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -402,7 +402,7 @@ def extract_array( >>> extract_array([1, 2, 3]) [1, 2, 3] - For an ndarray-backed Series / Index a PandasArray is returned. + For an ndarray-backed Series / Index the ndarray is returned. >>> extract_array(pd.Series([1, 2, 3])) array([1, 2, 3]) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 07c6a84f75302..7dc59bdb1e840 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -238,10 +238,11 @@ def _format_attrs(self): """ attrs = super()._format_attrs() for attrib in self._attributes: + # iterating over _attributes prevents us from doing this for PeriodIndex if attrib == "freq": freq = self.freqstr if freq is not None: - freq = repr(freq) + freq = repr(freq) # e.g. D -> 'D' # Argument 1 to "append" of "list" has incompatible type # "Tuple[str, Optional[str]]"; expected "Tuple[str, Union[str, int]]" attrs.append(("freq", freq)) # type: ignore[arg-type] diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 3b42d1c4505da..60179b69f56a4 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -642,7 +642,7 @@ def _get_setitem_indexer(self, key): self._ensure_listlike_indexer(key) if self.axis is not None: - return self._convert_tuple(key, is_setter=True) + return self._convert_tuple(key) ax = self.obj._get_axis(0) @@ -653,12 +653,12 @@ def _get_setitem_indexer(self, key): if isinstance(key, tuple): with suppress(IndexingError): - return self._convert_tuple(key, is_setter=True) + return self._convert_tuple(key) if isinstance(key, range): return list(key) - return self._convert_to_indexer(key, axis=0, is_setter=True) + return self._convert_to_indexer(key, axis=0) def _ensure_listlike_indexer(self, key, axis=None, value=None): """ @@ -755,21 +755,19 @@ def _is_nested_tuple_indexer(self, tup: tuple) -> bool: return any(is_nested_tuple(tup, ax) for ax in self.obj.axes) return False - def _convert_tuple(self, key, is_setter: bool = False): + def _convert_tuple(self, key): keyidx = [] if self.axis is not None: axis = self.obj._get_axis_number(self.axis) for i in range(self.ndim): if i == axis: - keyidx.append( - self._convert_to_indexer(key, axis=axis, is_setter=is_setter) - ) + keyidx.append(self._convert_to_indexer(key, axis=axis)) else: keyidx.append(slice(None)) else: self._validate_key_length(key) for i, k in enumerate(key): - idx = self._convert_to_indexer(k, axis=i, is_setter=is_setter) + idx = self._convert_to_indexer(k, axis=i) keyidx.append(idx) return tuple(keyidx) @@ -867,8 +865,8 @@ def _getitem_nested_tuple(self, tup: tuple): # a tuple passed to a series with a multi-index if len(tup) > self.ndim: if self.name != "loc": - # This should never be reached, but lets be explicit about it - raise ValueError("Too many indices") + # This should never be reached, but let's be explicit about it + raise ValueError("Too many indices") # pragma: no cover if all(is_hashable(x) or com.is_null_slice(x) for x in tup): # GH#10521 Series should reduce MultiIndex dimensions instead of # DataFrame, IndexingError is not raised when slice(None,None,None) @@ -911,7 +909,7 @@ def _getitem_nested_tuple(self, tup: tuple): return obj - def _convert_to_indexer(self, key, axis: int, is_setter: bool = False): + def _convert_to_indexer(self, key, axis: int): raise AbstractMethodError(self) def __getitem__(self, key): @@ -1176,7 +1174,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int): # return a DatetimeIndex instead of a slice object. return self.obj.take(indexer, axis=axis) - def _convert_to_indexer(self, key, axis: int, is_setter: bool = False): + def _convert_to_indexer(self, key, axis: int): """ Convert indexing key into something we can use to do actual fancy indexing on a ndarray. @@ -1486,7 +1484,7 @@ def _get_slice_axis(self, slice_obj: slice, axis: int): labels._validate_positional_slice(slice_obj) return self.obj._slice(slice_obj, axis=axis) - def _convert_to_indexer(self, key, axis: int, is_setter: bool = False): + def _convert_to_indexer(self, key, axis: int): """ Much simpler as we only have to deal with our valid types. """ diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index 34d0137c26fda..9bc2404cefcfa 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -672,6 +672,7 @@ def _combine_concat_plans(plans, concat_axis: int): offset += last_plc.as_slice.stop else: + # singleton list so we can modify it as a side-effect within _next_or_none num_ended = [0] def _next_or_none(seq): diff --git a/pandas/core/series.py b/pandas/core/series.py index 32b56462788e5..8c02049f1bc37 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4386,7 +4386,7 @@ def _reduce( return op(delegate, skipna=skipna, **kwds) def _reindex_indexer( - self, new_index: Index | None, indexer: np.ndarray | None, copy: bool + self, new_index: Index | None, indexer: npt.NDArray[np.intp] | None, copy: bool ) -> Series: # Note: new_index is None iff indexer is None # if not None, indexer is np.intp