Skip to content

Commit 94edc9f

Browse files
committed
refactor, use _can_hold_strings
1 parent 0d38f73 commit 94edc9f

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

pandas/core/indexes/base.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def _outer_indexer(self, left, right):
231231
_attributes = ["name"]
232232
_is_numeric_dtype = False
233233
_can_hold_na = True
234+
_can_hold_strings = True
234235

235236
# would we like our indexing holder to defer to us
236237
_defer_to_indexing = False
@@ -572,14 +573,18 @@ def _engine(self):
572573
@cache_readonly
573574
def _dir_additions_for_owner(self) -> Set[str_t]:
574575
"""
575-
Add the string-like attributes from this index to the owners' dir output.
576+
Add the string-like labels to the owner dataframe/series dir output.
577+
576578
If this is a MultiIndex, it's first level values are used.
577579
"""
578-
return {
579-
c
580-
for c in self.unique(level=0)[:100]
581-
if isinstance(c, str) and c.isidentifier()
582-
}
580+
if self._can_hold_strings:
581+
return {
582+
c
583+
for c in self.unique(level=0)[:100]
584+
if isinstance(c, str) and c.isidentifier()
585+
}
586+
else:
587+
return set()
583588

584589
# --------------------------------------------------------------------
585590
# Array-Like Methods

pandas/core/indexes/datetimelike.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Base and utility classes for tseries type pandas objects.
33
"""
44
from datetime import datetime, tzinfo
5-
from typing import TYPE_CHECKING, Any, List, Optional, Set, TypeVar, Union, cast
5+
from typing import TYPE_CHECKING, Any, List, Optional, TypeVar, Union, cast
66

77
import numpy as np
88

@@ -88,6 +88,7 @@ class DatetimeIndexOpsMixin(ExtensionIndex):
8888
Common ops mixin to support a unified interface datetimelike Index.
8989
"""
9090

91+
_can_hold_strings = False
9192
_data: Union[DatetimeArray, TimedeltaArray, PeriodArray]
9293
freq: Optional[BaseOffset]
9394
freqstr: Optional[str]
@@ -105,10 +106,6 @@ class DatetimeIndexOpsMixin(ExtensionIndex):
105106
def _is_all_dates(self) -> bool:
106107
return True
107108

108-
@cache_readonly
109-
def _dir_additions_for_owner(self) -> Set[str]:
110-
return set()
111-
112109
# ------------------------------------------------------------------------
113110
# Abstract data attributes
114111

pandas/core/indexes/interval.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from functools import wraps
33
from operator import le, lt
44
import textwrap
5-
from typing import TYPE_CHECKING, Any, List, Optional, Set, Tuple, Union, cast
5+
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union, cast
66

77
import numpy as np
88

@@ -193,6 +193,7 @@ class IntervalIndex(IntervalMixin, ExtensionIndex):
193193

194194
_data: IntervalArray
195195
_values: IntervalArray
196+
_can_hold_strings = True
196197

197198
# --------------------------------------------------------------------
198199
# Constructors
@@ -371,10 +372,6 @@ def __contains__(self, key: Any) -> bool:
371372
def _multiindex(self) -> MultiIndex:
372373
return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"])
373374

374-
@cache_readonly
375-
def _dir_additions_for_owner(self) -> Set[str]:
376-
return set()
377-
378375
@cache_readonly
379376
def values(self) -> IntervalArray:
380377
"""

pandas/core/indexes/numeric.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Set
1+
from typing import Any
22

33
import numpy as np
44

@@ -42,6 +42,7 @@ class NumericIndex(Index):
4242
_default_dtype: np.dtype
4343

4444
_is_numeric_dtype = True
45+
_can_hold_strings = False
4546

4647
def __new__(cls, data=None, dtype=None, copy=False, name=None):
4748
cls._validate_dtype(dtype)
@@ -184,10 +185,6 @@ def _union(self, other, sort):
184185
else:
185186
return super()._union(other, sort)
186187

187-
@cache_readonly
188-
def _dir_additions_for_owner(self) -> Set[str]:
189-
return set()
190-
191188

192189
_num_index_shared_docs[
193190
"class_descr"

0 commit comments

Comments
 (0)