Skip to content

Commit

Permalink
BUG: Series inferring new string dtype even if dtype is given for sca…
Browse files Browse the repository at this point in the history
…lar value (#55537)
  • Loading branch information
phofl authored Oct 22, 2023
1 parent ad3f3f7 commit 206f981
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ def sanitize_array(
-------
np.ndarray or ExtensionArray
"""
original_dtype = dtype
if isinstance(data, ma.MaskedArray):
data = sanitize_masked_array(data)

Expand All @@ -562,7 +563,11 @@ def sanitize_array(
if not is_list_like(data):
if index is None:
raise ValueError("index must be specified when data is not list-like")
if isinstance(data, str) and using_pyarrow_string_dtype():
if (
isinstance(data, str)
and using_pyarrow_string_dtype()
and original_dtype is None
):
from pandas.core.arrays.string_ import StringDtype

dtype = StringDtype("pyarrow_numpy")
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,14 @@ def test_series_string_inference_storage_definition(self):
result = Series(["a", "b"], dtype="string")
tm.assert_series_equal(result, expected)

def test_series_constructor_infer_string_scalar(self):
# GH#55537
with pd.option_context("future.infer_string", True):
ser = Series("a", index=[1, 2], dtype="string[python]")
expected = Series(["a", "a"], index=[1, 2], dtype="string[python]")
tm.assert_series_equal(ser, expected)
assert ser.dtype.storage == "python"


class TestSeriesConstructorIndexCoercion:
def test_series_constructor_datetimelike_index_coercion(self):
Expand Down

0 comments on commit 206f981

Please sign in to comment.