From 51f7d6a969429199102b4788d79d10f5721d9050 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 1 Aug 2019 05:56:48 -0700 Subject: [PATCH] Backport PR #27653: BUG: Fix dir(interval_index) --- doc/source/whatsnew/v0.25.1.rst | 2 +- pandas/_libs/lib.pyx | 1 + pandas/core/strings.py | 7 +++++-- pandas/tests/dtypes/test_inference.py | 11 +++++++++++ pandas/tests/indexes/interval/test_interval.py | 7 +++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.25.1.rst b/doc/source/whatsnew/v0.25.1.rst index fb67decb46b64..40fefe7ec43a8 100644 --- a/doc/source/whatsnew/v0.25.1.rst +++ b/doc/source/whatsnew/v0.25.1.rst @@ -78,7 +78,7 @@ Strings Interval ^^^^^^^^ - +- Bug in :class:`IntervalIndex` where `dir(obj)` would raise ``ValueError`` (:issue:`27571`) - - - diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 27ee685acfde7..fef17fd43a8e3 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -969,6 +969,7 @@ _TYPE_MAP = { 'M': 'datetime64', 'timedelta64[ns]': 'timedelta64', 'm': 'timedelta64', + 'interval': 'interval', } # types only exist on certain platform diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 7c293ca4e50b0..d4c6d780709a5 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -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!") diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 4d688976cd50b..6ab3830317059 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -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): diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index c61af1ce70aed..c1a21e6a7f152 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -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