Skip to content

BUG: Fix dir(interval_index) #27653

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
merged 8 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pandas.core.algorithms import take_1d
from pandas.core.base import NoNewAttributesMixin
import pandas.core.common as com
from pandas.core.construction import extract_array

_cpython_optimized_encoders = (
"utf-8",
Expand Down Expand Up @@ -1950,11 +1951,14 @@ def _validate(data):
# see _libs/lib.pyx for list of inferred types
allowed_types = ["string", "empty", "bytes", "mixed", "mixed-integer"]

values = getattr(data, "values", data) # Series / Index
values = extract_array(data) # unpack Series / Index
values = getattr(values, "categories", values) # categorical / normal

# missing values obfuscate type inference -> skip
inferred_dtype = lib.infer_dtype(values, skipna=True)
try:
# GH#27571 make sure this doesn't raise too early
inferred_dtype = lib.infer_dtype(values, skipna=True)
except (ValueError, TypeError):
inferred_dtype = None

if inferred_dtype not in allowed_types:
raise AttributeError("Can only use .str accessor with string values!")
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