Skip to content

Commit 1297c2b

Browse files
jrebackcbertinato
authored andcommitted
accomodate numpy scalar
1 parent 6f65134 commit 1297c2b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

pandas/core/series.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,18 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
234234
if not is_list_like(data):
235235
data = [data]
236236
index = com._default_index(len(data))
237-
elif is_list_like(data) and len(index) != len(data):
238-
raise ValueError('Length of passed values is {val}, '
239-
'index implies {ind}'
240-
.format(val=len(data), ind=len(index)))
237+
elif is_list_like(data):
238+
239+
# a scalar numpy array is list-like but doesn't
240+
# have a proper length
241+
try:
242+
if len(index) != len(data):
243+
raise ValueError(
244+
'Length of passed values is {val}, '
245+
'index implies {ind}'
246+
.format(val=len(data), ind=len(index)))
247+
except TypeError:
248+
pass
241249

242250
# create/copy the manager
243251
if isinstance(data, SingleBlockManager):

pandas/tests/series/test_constructors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,14 @@ def test_constructor_index_mismatch(self, input):
414414
with pytest.raises(ValueError, message=msg):
415415
Series(input, index=np.arange(4))
416416

417+
def test_constructor_numpy_scalar(self):
418+
# GH 19342
419+
# construction with a numpy scalar
420+
# should not raise
421+
result = Series(np.array(100), index=np.arange(4))
422+
expected = Series(100, index=np.arange(4))
423+
tm.assert_series_equal(result, expected)
424+
417425
def test_constructor_corner(self):
418426
df = tm.makeTimeDataFrame()
419427
objs = [df, df]

0 commit comments

Comments
 (0)