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 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 @@ -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 @@ -1953,8 +1953,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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

infer_dtype should never raise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib.pyx L1207 specifically raises ValueError

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, can you make an issue to fix the doc-string in infer_dtype. is possible to handle this inside infer_dtype rather than raise? or is this meant as a tactical fix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is possible to handle this inside infer_dtype rather than raise? or is this meant as a tactical fix?

the interval case is fixed by this PR inside infer_dtype, so catching the error here is just a dir-should-never-raise thing. the more general infer_dtype fixes are part of a larger push to recognize EADtypes correctly, yah

# 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 @@ -1153,6 +1153,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