From 75ef96fb79eb72c89583e59f290c6f934dca5e5c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 18 Dec 2019 14:54:26 -0800 Subject: [PATCH] BUG: raise on non-hashable Index name, closes #29069 --- pandas/core/indexes/base.py | 17 +++++++++++++++-- pandas/core/indexes/category.py | 5 ++--- pandas/core/indexes/datetimes.py | 5 ++--- pandas/core/indexes/interval.py | 4 ++-- pandas/core/indexes/numeric.py | 10 +++++++--- pandas/core/indexes/period.py | 9 ++++++--- pandas/core/indexes/range.py | 4 ++-- pandas/core/indexes/timedeltas.py | 3 ++- pandas/tests/indexes/common.py | 7 +++++++ pandas/tests/indexes/test_base.py | 1 + 10 files changed, 46 insertions(+), 19 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 5abd049b9564c..f8068fb2a864c 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -274,8 +274,7 @@ def __new__( from .interval import IntervalIndex from .category import CategoricalIndex - if name is None and hasattr(data, "name"): - name = data.name + name = maybe_extract_name(name, data) if isinstance(data, ABCPandasArray): # ensure users don't accidentally put a PandasArray in an index. @@ -5464,3 +5463,17 @@ def default_index(n): from pandas.core.indexes.range import RangeIndex return RangeIndex(0, n, name=None) + + +def maybe_extract_name(name, obj): + """ + If no name is passed, then extract it from data, validating hashability. + """ + if name is None and hasattr(obj, "name"): + name = obj.name + + # GH#29069 + if not is_hashable(name): + raise TypeError(f"Index.name must be a hashable type") + + return name diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 44478d00da9cf..aedf6fa31fdae 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -29,7 +29,7 @@ from pandas.core.base import _shared_docs import pandas.core.common as com import pandas.core.indexes.base as ibase -from pandas.core.indexes.base import Index, _index_shared_docs +from pandas.core.indexes.base import Index, _index_shared_docs, maybe_extract_name import pandas.core.missing as missing from pandas.core.ops import get_op_result_name @@ -175,8 +175,7 @@ def __new__( dtype = CategoricalDtype._from_values_or_dtype(data, categories, ordered, dtype) - if name is None and hasattr(data, "name"): - name = data.name + name = maybe_extract_name(name, data) if not is_categorical_dtype(data): # don't allow scalars diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 523c434cb7377..6c68432fddb92 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -30,7 +30,7 @@ ) from pandas.core.base import _shared_docs import pandas.core.common as com -from pandas.core.indexes.base import Index +from pandas.core.indexes.base import Index, maybe_extract_name from pandas.core.indexes.datetimelike import ( DatetimeIndexOpsMixin, DatetimelikeDelegateMixin, @@ -253,8 +253,7 @@ def __new__( # - Cases checked above all return/raise before reaching here - # - if name is None and hasattr(data, "name"): - name = data.name + name = maybe_extract_name(name, data) dtarr = DatetimeArray._from_sequence( data, diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index f046f0d89c428..8c6eb64ae9943 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -47,6 +47,7 @@ _index_shared_docs, default_pprint, ensure_index, + maybe_extract_name, ) from pandas.core.indexes.datetimes import DatetimeIndex, date_range from pandas.core.indexes.multi import MultiIndex @@ -213,8 +214,7 @@ def __new__( cls, data, closed=None, dtype=None, copy=False, name=None, verify_integrity=True ): - if name is None and hasattr(data, "name"): - name = data.name + name = maybe_extract_name(name, data) with rewrite_exception("IntervalArray", cls.__name__): array = IntervalArray( diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 048bff46759bc..ee04ee3918c7a 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -30,7 +30,12 @@ from pandas._typing import Dtype from pandas.core import algorithms import pandas.core.common as com -from pandas.core.indexes.base import Index, InvalidIndexError, _index_shared_docs +from pandas.core.indexes.base import ( + Index, + InvalidIndexError, + _index_shared_docs, + maybe_extract_name, +) from pandas.core.ops import get_op_result_name _num_index_shared_docs = dict() @@ -68,8 +73,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None): else: subarr = data - if name is None and hasattr(data, "name"): - name = data.name + name = maybe_extract_name(name, data) return cls._simple_new(subarr, name=name) @classmethod diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 9485116a8084a..6ef55d79e20c5 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -25,7 +25,11 @@ from pandas.core.base import _shared_docs import pandas.core.common as com import pandas.core.indexes.base as ibase -from pandas.core.indexes.base import _index_shared_docs, ensure_index +from pandas.core.indexes.base import ( + _index_shared_docs, + ensure_index, + maybe_extract_name, +) from pandas.core.indexes.datetimelike import ( DatetimeIndexOpsMixin, DatetimelikeDelegateMixin, @@ -184,8 +188,7 @@ def __new__( argument = list(set(fields) - valid_field_set)[0] raise TypeError(f"__new__() got an unexpected keyword argument {argument}") - if name is None and hasattr(data, "name"): - name = data.name + name = maybe_extract_name(name, data) if data is None and ordinal is None: # range-based. diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 6ad70841a48b0..68cb03891bafe 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -26,7 +26,7 @@ import pandas.core.common as com from pandas.core.construction import extract_array import pandas.core.indexes.base as ibase -from pandas.core.indexes.base import Index, _index_shared_docs +from pandas.core.indexes.base import Index, _index_shared_docs, maybe_extract_name from pandas.core.indexes.numeric import Int64Index from pandas.core.ops.common import unpack_zerodim_and_defer @@ -85,10 +85,10 @@ def __new__( ): cls._validate_dtype(dtype) + name = maybe_extract_name(name, start) # RangeIndex if isinstance(start, RangeIndex): - name = start.name if name is None else name start = start._range return cls._simple_new(start, dtype=dtype, name=name) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 889075ebe4e31..f4f2cb31cccde 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -25,7 +25,7 @@ from pandas.core.arrays.timedeltas import TimedeltaArray, _is_convertible_to_td from pandas.core.base import _shared_docs import pandas.core.common as com -from pandas.core.indexes.base import Index, _index_shared_docs +from pandas.core.indexes.base import Index, _index_shared_docs, maybe_extract_name from pandas.core.indexes.datetimelike import ( DatetimeIndexOpsMixin, DatetimelikeDelegateMixin, @@ -163,6 +163,7 @@ def __new__( copy=False, name=None, ): + name = maybe_extract_name(name, data) if is_scalar(data): raise TypeError( diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 102949fe3f05e..68cca473d6bb0 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -103,6 +103,13 @@ def test_shift(self): with pytest.raises(NotImplementedError, match=msg): idx.shift(1, 2) + def test_constructor_name_unhashable(self): + # GH#29069 check that name is hashable + # See also same-named test in tests.series.test_constructors + idx = self.create_index() + with pytest.raises(TypeError, match="Index.name must be a hashable type"): + type(idx)(idx, name=[]) + def test_create_index_existing_name(self): # GH11193, when an existing index is passed, and a new name is not diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index d4d644e486478..6ec35a32d74ce 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -100,6 +100,7 @@ def test_constructor_copy(self, index): arr[0] = "SOMEBIGLONGSTRING" assert new_index[0] != "SOMEBIGLONGSTRING" + # FIXME: dont leave commented-out # what to do here? # arr = np.array(5.) # pytest.raises(Exception, arr.view, Index)