Skip to content

Commit 98f1f16

Browse files
committed
BUG: Fix Series constructor for Categorical with index
Fixes Series constructor so that ValueError is raised when a Categorical and index of different length are given.
1 parent 8768876 commit 98f1f16

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ Reshaping
902902
- :func:`Series.rename` now accepts ``axis`` as a kwarg (:issue:`18589`)
903903
- Comparisons between :class:`Series` and :class:`Index` would return a ``Series`` with an incorrect name, ignoring the ``Index``'s name attribute (:issue:`19582`)
904904
- Bug in :func:`qcut` where datetime and timedelta data with ``NaT`` present raised a ``ValueError`` (:issue:`19768`)
905+
- Bug in :class:`Series` constructor with ``Categorical`` where a ```ValueError`` is not raised when an index of different length is given (:issue:`19342`)
905906

906907
Other
907908
^^^^^

pandas/core/series.py

+6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
210210
raise ValueError("cannot specify a dtype with a "
211211
"Categorical unless "
212212
"dtype='category'")
213+
213214
elif (isinstance(data, types.GeneratorType) or
214215
(compat.PY3 and isinstance(data, map))):
215216
data = list(data)
@@ -226,6 +227,11 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
226227
if not is_list_like(data):
227228
data = [data]
228229
index = com._default_index(len(data))
230+
else:
231+
if not is_scalar(data) and len(index) != len(data):
232+
raise ValueError('Length of passed values is {val}, '
233+
'index implies {ind}'
234+
.format(val=len(data), ind=len(index)))
229235

230236
# create/copy the manager
231237
if isinstance(data, SingleBlockManager):

pandas/tests/series/test_constructors.py

+14
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,20 @@ def test_constructor_default_index(self):
393393
s = Series([0, 1, 2])
394394
tm.assert_index_equal(s.index, pd.Index(np.arange(3)))
395395

396+
@pytest.mark.parametrize('input', [[1, 2, 3],
397+
(1, 2, 3),
398+
list(range(3)),
399+
pd.Categorical(['a', 'b', 'a']),
400+
(i for i in range(3)),
401+
map(lambda x: x, range(3))])
402+
def test_constructor_index_mismatch(self, input):
403+
# GH 19342
404+
# test that construction of a Series with an index of different length
405+
# raises an error
406+
msg = 'Length of passed values is 3, index implies 4'
407+
with pytest.raises(ValueError, message=msg):
408+
Series(input, index=np.arange(4))
409+
396410
def test_constructor_corner(self):
397411
df = tm.makeTimeDataFrame()
398412
objs = [df, df]

0 commit comments

Comments
 (0)