Skip to content

Commit abe385d

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 c1f0c63 commit abe385d

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ Reshaping
844844
- Improved error message for :func:`DataFrame.merge` when there is no common merge key (:issue:`19427`)
845845
- Bug in :func:`DataFrame.join` which does an *outer* instead of a *left* join when being called with multiple DataFrames and some have non-unique indices (:issue:`19624`)
846846
- :func:`Series.rename` now accepts ``axis`` as a kwarg (:issue:`18589`)
847+
- Bug in :class:`Series` constructor with ``Categorical`` where an error is not raised when an index of different length is given (:issue:`19342`)
847848

848849
Other
849850
^^^^^

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)
@@ -238,6 +239,11 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
238239
data = _sanitize_array(data, index, dtype, copy,
239240
raise_cast_failure=True)
240241

242+
if index is not None and len(index) != len(data):
243+
raise ValueError('Length of passed values is {val}, '
244+
'index implies {ind}'
245+
.format(val=len(data), ind=len(index)))
246+
241247
data = SingleBlockManager(data, index, fastpath=True)
242248

243249
generic.NDFrame.__init__(self, data, fastpath=True)

pandas/tests/series/test_constructors.py

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from datetime import datetime, timedelta
77
from collections import OrderedDict
8+
import types
89

910
from numpy import nan
1011
import numpy as np
@@ -22,6 +23,7 @@
2223
from pandas._libs import lib
2324
from pandas._libs.tslib import iNaT
2425

26+
from pandas import compat
2527
from pandas.compat import lrange, range, zip, long
2628
from pandas.util.testing import assert_series_equal
2729
import pandas.util.testing as tm
@@ -393,6 +395,20 @@ def test_constructor_default_index(self):
393395
s = Series([0, 1, 2])
394396
tm.assert_index_equal(s.index, pd.Index(np.arange(3)))
395397

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

0 commit comments

Comments
 (0)