diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index acbfa2eb3ccac..3f39d52eb832e 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -1035,6 +1035,7 @@ Reshaping - Bug in :class:`Series` constructor with ``Categorical`` where a ```ValueError`` is not raised when an index of different length is given (:issue:`19342`) - Bug in :meth:`DataFrame.astype` where column metadata is lost when converting to categorical or a dictionary of dtypes (:issue:`19920`) - Bug in :func:`cut` and :func:`qcut` where timezone information was dropped (:issue:`19872`) +- Bug in :class:`Series` constructor with a ``dtype=str``, previously raised in some cases (:issue:`19853`) Other ^^^^^ diff --git a/pandas/core/series.py b/pandas/core/series.py index e4801242073a2..bcf97832e40ce 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4059,9 +4059,10 @@ def _try_cast(arr, take_fast_path): if issubclass(subarr.dtype.type, compat.string_types): # GH 16605 # If not empty convert the data to dtype - if not isna(data).all(): - data = np.array(data, dtype=dtype, copy=False) - - subarr = np.array(data, dtype=object, copy=copy) + # GH 19853: If data is a scalar, subarr has already the result + if not is_scalar(data): + if not np.all(isna(data)): + data = np.array(data, dtype=dtype, copy=False) + subarr = np.array(data, dtype=object, copy=copy) return subarr diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index e0bfe41645a3f..82b5b1c10fa2d 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -110,6 +110,11 @@ def test_constructor_empty(self, input_class): empty2 = Series(input_class(), index=lrange(10), dtype='float64') assert_series_equal(empty, empty2) + # GH 19853 : with empty string, index and dtype str + empty = Series('', dtype=str, index=range(3)) + empty2 = Series('', index=range(3)) + assert_series_equal(empty, empty2) + @pytest.mark.parametrize('input_arg', [np.nan, float('nan')]) def test_constructor_nan(self, input_arg): empty = Series(dtype='float64', index=lrange(10))