Skip to content

Commit 5508704

Browse files
cbertinatoharisbal
authored and
harisbal
committed
BUG: Fix Series constructor for Categorical with index (pandas-dev#19714)
1 parent 3471271 commit 5508704

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ Reshaping
919919
- Comparisons between :class:`Series` and :class:`Index` would return a ``Series`` with an incorrect name, ignoring the ``Index``'s name attribute (:issue:`19582`)
920920
- Bug in :func:`qcut` where datetime and timedelta data with ``NaT`` present raised a ``ValueError`` (:issue:`19768`)
921921
- Bug in :func:`DataFrame.iterrows`, which would infers strings not compliant to `ISO8601 <https://en.wikipedia.org/wiki/ISO_8601>`_ to datetimes (:issue:`19671`)
922+
- Bug in :class:`Series` constructor with ``Categorical`` where a ```ValueError`` is not raised when an index of different length is given (:issue:`19342`)
922923

923924
Other
924925
^^^^^

pandas/core/series.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
212212
'be False.')
213213

214214
elif is_extension_array_dtype(data) and dtype is not None:
215-
# GH12574: Allow dtype=category only, otherwise error
216215
if not data.dtype.is_dtype(dtype):
217216
raise ValueError("Cannot specify a dtype '{}' with an "
218217
"extension array of a different "
@@ -235,6 +234,18 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
235234
if not is_list_like(data):
236235
data = [data]
237236
index = com._default_index(len(data))
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
238249

239250
# create/copy the manager
240251
if isinstance(data, SingleBlockManager):

pandas/tests/io/formats/test_style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def setup_method(self, method):
2424

2525
def h(x, foo='bar'):
2626
return pd.Series(
27-
['color: {foo}'.format(foo=foo)], index=x.index, name=x.name)
27+
'color: {foo}'.format(foo=foo), index=x.index, name=x.name)
2828

2929
self.h = h
3030
self.styler = Styler(self.df)

pandas/tests/series/test_constructors.py

+28
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,34 @@ def test_constructor_default_index(self):
400400
s = Series([0, 1, 2])
401401
tm.assert_index_equal(s.index, pd.Index(np.arange(3)))
402402

403+
@pytest.mark.parametrize('input', [[1, 2, 3],
404+
(1, 2, 3),
405+
list(range(3)),
406+
pd.Categorical(['a', 'b', 'a']),
407+
(i for i in range(3)),
408+
map(lambda x: x, range(3))])
409+
def test_constructor_index_mismatch(self, input):
410+
# GH 19342
411+
# test that construction of a Series with an index of different length
412+
# raises an error
413+
msg = 'Length of passed values is 3, index implies 4'
414+
with pytest.raises(ValueError, message=msg):
415+
Series(input, index=np.arange(4))
416+
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), dtype='int64')
422+
expected = Series(100, index=np.arange(4), dtype='int64')
423+
tm.assert_series_equal(result, expected)
424+
425+
def test_constructor_broadcast_list(self):
426+
# GH 19342
427+
# construction with single-element container and index
428+
# should raise
429+
pytest.raises(ValueError, Series, ['foo'], index=['a', 'b', 'c'])
430+
403431
def test_constructor_corner(self):
404432
df = tm.makeTimeDataFrame()
405433
objs = [df, df]

0 commit comments

Comments
 (0)