Skip to content

Commit

Permalink
BUG: Fix dir(interval_index) (pandas-dev#27653)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and quintusdias committed Aug 15, 2019
1 parent 855251b commit 1338b91
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
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 @@ -925,6 +925,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 @@ -1961,8 +1961,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 @@ -1168,6 +1168,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

0 comments on commit 1338b91

Please sign in to comment.