Skip to content

Commit c81d90f

Browse files
authored
ERR: Raise a better error for numpy singletons in Index (#33026)
1 parent a3097b5 commit c81d90f

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ Indexing
336336
- Bug in :meth:`DataFrame.iloc.__setitem__` on a :class:`DataFrame` with duplicate columns incorrectly setting values for all matching columns (:issue:`15686`, :issue:`22036`)
337337
- Bug in :meth:`DataFrame.loc:` and :meth:`Series.loc` with a :class:`DatetimeIndex`, :class:`TimedeltaIndex`, or :class:`PeriodIndex` incorrectly allowing lookups of non-matching datetime-like dtypes (:issue:`32650`)
338338
- Bug in :meth:`Series.__getitem__` indexing with non-standard scalars, e.g. ``np.dtype`` (:issue:`32684`)
339+
- Bug in :class:`Index` constructor where an unhelpful error message was raised for ``numpy`` scalars (:issue:`33017`)
339340

340341
Missing
341342
^^^^^^^

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,10 @@ def __new__(
395395
raise ValueError("Index data must be 1-dimensional")
396396
return cls._simple_new(subarr, name)
397397

398-
elif hasattr(data, "__array__"):
399-
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name, **kwargs)
400398
elif data is None or is_scalar(data):
401399
raise cls._scalar_data_error(data)
400+
elif hasattr(data, "__array__"):
401+
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name, **kwargs)
402402
else:
403403
if tupleize_cols and is_list_like(data):
404404
# GH21470: convert iterable to list before determining if empty

pandas/tests/indexes/base_class/test_constructors.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import pytest
23

34
from pandas import Index, MultiIndex
@@ -7,14 +8,15 @@ class TestIndexConstructor:
78
# Tests for the Index constructor, specifically for cases that do
89
# not return a subclass
910

10-
def test_constructor_corner(self):
11+
@pytest.mark.parametrize("value", [1, np.int64(1)])
12+
def test_constructor_corner(self, value):
1113
# corner case
1214
msg = (
1315
r"Index\(\.\.\.\) must be called with a collection of some "
14-
"kind, 0 was passed"
16+
f"kind, {value} was passed"
1517
)
1618
with pytest.raises(TypeError, match=msg):
17-
Index(0)
19+
Index(value)
1820

1921
@pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])
2022
def test_construction_list_mixed_tuples(self, index_vals):

0 commit comments

Comments
 (0)