Skip to content

Commit 9e33bf7

Browse files
jbrockmendelWillAyd
authored andcommitted
BUG: compound dtype ndarray passed to Series (#30494)
1 parent ad2790c commit 9e33bf7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pandas/core/series.py

+7
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ def __init__(
235235
copy = False
236236

237237
elif isinstance(data, np.ndarray):
238+
if len(data.dtype):
239+
# GH#13296 we are dealing with a compound dtype, which
240+
# should be treated as 2D
241+
raise ValueError(
242+
"Cannot construct a Series from an ndarray with "
243+
"compound dtype. Use DataFrame instead."
244+
)
238245
pass
239246
elif isinstance(data, ABCSeries):
240247
if name is None:

pandas/tests/series/test_constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def test_invalid_dtype(self):
6767
with pytest.raises(TypeError, match=msg):
6868
Series([], name="time", dtype=dtype)
6969

70+
def test_invalid_compound_dtype(self):
71+
# GH#13296
72+
c_dtype = np.dtype([("a", "i8"), ("b", "f4")])
73+
cdt_arr = np.array([(1, 0.4), (256, -13)], dtype=c_dtype)
74+
75+
with pytest.raises(ValueError, match="Use DataFrame instead"):
76+
Series(cdt_arr, index=["A", "B"])
77+
7078
def test_scalar_conversion(self):
7179

7280
# Pass in scalar is disabled

0 commit comments

Comments
 (0)