Skip to content

Backport PR #27653 on branch 0.25.x (BUG: Fix dir(interval_index)) #27693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Strings

Interval
^^^^^^^^

- Bug in :class:`IntervalIndex` where `dir(obj)` would raise ``ValueError`` (:issue:`27571`)
-
-
-
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ _TYPE_MAP = {
'M': 'datetime64',
'timedelta64[ns]': 'timedelta64',
'm': 'timedelta64',
'interval': 'interval',
}

# types only exist on certain platform
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,8 +1957,11 @@ def _validate(data):
values = getattr(data, "values", data) # Series / Index
values = getattr(values, "categories", values) # categorical / normal

# missing values obfuscate type inference -> skip
inferred_dtype = lib.infer_dtype(values, skipna=True)
try:
inferred_dtype = lib.infer_dtype(values, skipna=True)
except ValueError:
# GH#27571 mostly occurs with ExtensionArray
inferred_dtype = None

if inferred_dtype not in allowed_types:
raise AttributeError("Can only use .str accessor with string " "values!")
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,17 @@ def test_categorical(self):
result = lib.infer_dtype(Series(arr), skipna=True)
assert result == "categorical"

def test_interval(self):
idx = pd.IntervalIndex.from_breaks(range(5), closed="both")
inferred = lib.infer_dtype(idx, skipna=False)
assert inferred == "interval"

inferred = lib.infer_dtype(idx._data, skipna=False)
assert inferred == "interval"

inferred = lib.infer_dtype(pd.Series(idx), skipna=False)
assert inferred == "interval"


class TestNumberScalar:
def test_is_number(self):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,10 @@ def test_is_all_dates(self):
)
year_2017_index = pd.IntervalIndex([year_2017])
assert not year_2017_index.is_all_dates


def test_dir():
# GH#27571 dir(interval_index) should not raise
index = IntervalIndex.from_arrays([0, 1], [1, 2])
result = dir(index)
assert "str" not in result