Skip to content

Commit 26dd5b1

Browse files
cbertinatojreback
authored andcommitted
BUG: fix Series constructor for scalar and Categorical dtype (#19717)
1 parent fdc0f25 commit 26dd5b1

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

doc/source/whatsnew/v0.23.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,8 @@ Categorical
730730
``self`` but in a different order (:issue:`19551`)
731731
- Bug in :meth:`Index.astype` with a categorical dtype where the resultant index is not converted to a :class:`CategoricalIndex` for all types of index (:issue:`18630`)
732732
- Bug in :meth:`Series.astype` and ``Categorical.astype()`` where an existing categorical data does not get updated (:issue:`10696`, :issue:`18593`)
733-
- Bug in :class:`Index` constructor with ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (:issue:`19032`)
733+
- Bug in :class:`Index` constructor with ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (issue:`19032`)
734+
- Bug in :class:`Series` constructor with scalar and ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (issue:`19565`)
734735

735736
Datetimelike
736737
^^^^^^^^^^^^

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
11781178
subarr = DatetimeIndex([value] * length, dtype=dtype)
11791179
elif is_categorical_dtype(dtype):
11801180
from pandas import Categorical
1181-
subarr = Categorical([value] * length)
1181+
subarr = Categorical([value] * length, dtype=dtype)
11821182
else:
11831183
if not isinstance(dtype, (np.dtype, type(np.dtype))):
11841184
dtype = dtype.dtype

pandas/tests/dtypes/test_cast.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
maybe_convert_string_to_object,
2323
maybe_convert_scalar,
2424
find_common_type,
25-
construct_1d_object_array_from_listlike)
25+
construct_1d_object_array_from_listlike,
26+
construct_1d_arraylike_from_scalar)
2627
from pandas.core.dtypes.dtypes import (
2728
CategoricalDtype,
2829
DatetimeTZDtype,
@@ -422,3 +423,15 @@ def test_cast_1d_array(self, datum1, datum2):
422423
@pytest.mark.parametrize('val', [1, 2., None])
423424
def test_cast_1d_array_invalid_scalar(self, val):
424425
pytest.raises(TypeError, construct_1d_object_array_from_listlike, val)
426+
427+
def test_cast_1d_arraylike_from_scalar_categorical(self):
428+
# GH 19565 - Categorical result from scalar did not maintain categories
429+
# and ordering of the passed dtype
430+
cats = ['a', 'b', 'c']
431+
cat_type = CategoricalDtype(categories=cats, ordered=False)
432+
expected = pd.Categorical(['a', 'a'], categories=cats)
433+
result = construct_1d_arraylike_from_scalar('a', len(expected),
434+
cat_type)
435+
tm.assert_categorical_equal(result, expected,
436+
check_category_order=True,
437+
check_dtype=True)

pandas/tests/series/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ def test_constructor_categorical_dtype(self):
270270
tm.assert_index_equal(result.cat.categories, pd.Index(['b', 'a']))
271271
assert result.cat.ordered is False
272272

273+
# GH 19565 - Check broadcasting of scalar with Categorical dtype
274+
result = Series('a', index=[0, 1],
275+
dtype=CategoricalDtype(['a', 'b'], ordered=True))
276+
expected = Series(['a', 'a'], index=[0, 1],
277+
dtype=CategoricalDtype(['a', 'b'], ordered=True))
278+
tm.assert_series_equal(result, expected, check_categorical=True)
279+
273280
def test_categorical_sideeffects_free(self):
274281
# Passing a categorical to a Series and then changing values in either
275282
# the series or the categorical should not change the values in the

0 commit comments

Comments
 (0)