Skip to content

Commit 678bfae

Browse files
authored
CLN: assorted cleanups in indexes/ (#31674)
1 parent d3b7e64 commit 678bfae

File tree

6 files changed

+13
-29
lines changed

6 files changed

+13
-29
lines changed

pandas/core/indexes/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4107,6 +4107,11 @@ def __contains__(self, key: Any) -> bool:
41074107
bool
41084108
Whether the key search is in the index.
41094109
4110+
Raises
4111+
------
4112+
TypeError
4113+
If the key is not hashable.
4114+
41104115
See Also
41114116
--------
41124117
Index.isin : Returns an ndarray of boolean dtype indicating whether the

pandas/core/indexes/category.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,6 @@ class CategoricalIndex(ExtensionIndex, accessor.PandasDelegate):
160160

161161
_typ = "categoricalindex"
162162

163-
_raw_inherit = {
164-
"argsort",
165-
"_internal_get_values",
166-
"tolist",
167-
"codes",
168-
"categories",
169-
"ordered",
170-
"_reverse_indexer",
171-
"searchsorted",
172-
}
173-
174163
codes: np.ndarray
175164
categories: Index
176165
_data: Categorical
@@ -847,18 +836,13 @@ def _concat_same_dtype(self, to_concat, name):
847836
result.name = name
848837
return result
849838

850-
def _delegate_property_get(self, name: str, *args, **kwargs):
851-
""" method delegation to the ._values """
852-
prop = getattr(self._values, name)
853-
return prop # no wrapping for now
854-
855839
def _delegate_method(self, name: str, *args, **kwargs):
856840
""" method delegation to the ._values """
857841
method = getattr(self._values, name)
858842
if "inplace" in kwargs:
859843
raise ValueError("cannot use inplace with CategoricalIndex")
860844
res = method(*args, **kwargs)
861-
if is_scalar(res) or name in self._raw_inherit:
845+
if is_scalar(res):
862846
return res
863847
return CategoricalIndex(res, name=self.name)
864848

pandas/core/indexes/datetimelike.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def sort_values(self, return_indexer=False, ascending=True):
200200
arr = type(self._data)._simple_new(
201201
sorted_values, dtype=self.dtype, freq=freq
202202
)
203-
return self._simple_new(arr, name=self.name)
203+
return type(self)._simple_new(arr, name=self.name)
204204

205205
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
206206
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
@@ -526,7 +526,7 @@ def _concat_same_dtype(self, to_concat, name):
526526
if is_diff_evenly_spaced:
527527
new_data._freq = self.freq
528528

529-
return self._simple_new(new_data, name=name)
529+
return type(self)._simple_new(new_data, name=name)
530530

531531
def shift(self, periods=1, freq=None):
532532
"""
@@ -629,7 +629,7 @@ def _shallow_copy(self, values=None, **kwargs):
629629
del attributes["freq"]
630630

631631
attributes.update(kwargs)
632-
return self._simple_new(values, **attributes)
632+
return type(self)._simple_new(values, **attributes)
633633

634634
# --------------------------------------------------------------------
635635
# Set Operation Methods
@@ -886,7 +886,7 @@ def _wrap_joined_index(self, joined, other):
886886
kwargs = {}
887887
if hasattr(self, "tz"):
888888
kwargs["tz"] = getattr(other, "tz", None)
889-
return self._simple_new(joined, name, **kwargs)
889+
return type(self)._simple_new(joined, name, **kwargs)
890890

891891
# --------------------------------------------------------------------
892892
# List-Like Methods

pandas/core/indexes/datetimes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ def _partial_date_slice(
590590
raise KeyError
591591

592592
# a monotonic (sorted) series can be sliced
593+
# Use asi8.searchsorted to avoid re-validating
593594
left = stamps.searchsorted(t1.value, side="left") if use_lhs else None
594595
right = stamps.searchsorted(t2.value, side="right") if use_rhs else None
595596

pandas/core/indexes/period.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from pandas._libs import index as libindex
8-
from pandas._libs.tslibs import NaT, frequencies as libfrequencies, resolution
8+
from pandas._libs.tslibs import frequencies as libfrequencies, resolution
99
from pandas._libs.tslibs.parsing import parse_time_string
1010
from pandas._libs.tslibs.period import Period
1111
from pandas.util._decorators import Appender, cache_readonly
@@ -547,7 +547,7 @@ def get_loc(self, key, method=None, tolerance=None):
547547
# we cannot construct the Period
548548
raise KeyError(key)
549549

550-
ordinal = key.ordinal if key is not NaT else key.value
550+
ordinal = self._data._unbox_scalar(key)
551551
try:
552552
return self._engine.get_loc(ordinal)
553553
except KeyError:

pandas/core/indexes/timedeltas.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,6 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
278278

279279
return label
280280

281-
def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
282-
# TODO: Check for non-True use_lhs/use_rhs
283-
assert isinstance(key, str), type(key)
284-
# given a key, try to figure out a location for a partial slice
285-
raise NotImplementedError
286-
287281
def is_type_compatible(self, typ) -> bool:
288282
return typ == self.inferred_type or typ == "timedelta"
289283

0 commit comments

Comments
 (0)