Skip to content

Commit 206f981

Browse files
authored
BUG: Series inferring new string dtype even if dtype is given for scalar value (#55537)
1 parent ad3f3f7 commit 206f981

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/construction.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ def sanitize_array(
540540
-------
541541
np.ndarray or ExtensionArray
542542
"""
543+
original_dtype = dtype
543544
if isinstance(data, ma.MaskedArray):
544545
data = sanitize_masked_array(data)
545546

@@ -562,7 +563,11 @@ def sanitize_array(
562563
if not is_list_like(data):
563564
if index is None:
564565
raise ValueError("index must be specified when data is not list-like")
565-
if isinstance(data, str) and using_pyarrow_string_dtype():
566+
if (
567+
isinstance(data, str)
568+
and using_pyarrow_string_dtype()
569+
and original_dtype is None
570+
):
566571
from pandas.core.arrays.string_ import StringDtype
567572

568573
dtype = StringDtype("pyarrow_numpy")

pandas/tests/series/test_constructors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,14 @@ def test_series_string_inference_storage_definition(self):
21232123
result = Series(["a", "b"], dtype="string")
21242124
tm.assert_series_equal(result, expected)
21252125

2126+
def test_series_constructor_infer_string_scalar(self):
2127+
# GH#55537
2128+
with pd.option_context("future.infer_string", True):
2129+
ser = Series("a", index=[1, 2], dtype="string[python]")
2130+
expected = Series(["a", "a"], index=[1, 2], dtype="string[python]")
2131+
tm.assert_series_equal(ser, expected)
2132+
assert ser.dtype.storage == "python"
2133+
21262134

21272135
class TestSeriesConstructorIndexCoercion:
21282136
def test_series_constructor_datetimelike_index_coercion(self):

0 commit comments

Comments
 (0)