diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index fa959f651d135..e44ac4d900d9e 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -1107,3 +1107,4 @@ Bug Fixes - Bug in ``crosstab`` where arguments with non-overlapping indexes would return a ``KeyError`` (:issue:`10291`) - Bug in ``DataFrame.apply`` in which reduction was not being prevented for cases in which ``dtype`` was not a numpy dtype (:issue:`12244`) +- Bug when initializing categorical series with a scalar value. (:issue:`12336`) \ No newline at end of file diff --git a/pandas/core/series.py b/pandas/core/series.py index 286a2ba79585a..1e5e0f6fb4553 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2926,6 +2926,8 @@ def create_from_value(value, index, dtype): if is_datetimetz(dtype): subarr = DatetimeIndex([value] * len(index), dtype=dtype) + elif is_categorical_dtype(dtype): + subarr = Categorical([value] * len(index)) else: if not isinstance(dtype, (np.dtype, type(np.dtype))): dtype = dtype.dtype diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 1289c0da14dd5..a8e592b5d13ce 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -635,6 +635,10 @@ def test_fromValue(self): self.assertEqual(dates.dtype, 'M8[ns]') self.assertEqual(len(dates), len(self.ts)) + categorical = Series(0, index=self.ts.index, dtype="category") + self.assertEqual(categorical.dtype, 'category') + self.assertEqual(len(categorical), len(self.ts)) + def test_constructor_dtype_timedelta64(self): # basic