Skip to content

Commit ad2e59a

Browse files
String dtype: disallow specifying the 'str' dtype with storage in [..] in string alias
1 parent 1be2637 commit ad2e59a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pandas/core/dtypes/dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,7 @@ def construct_from_string(cls, string: str) -> ArrowDtype:
23392339
)
23402340
if not string.endswith("[pyarrow]"):
23412341
raise TypeError(f"'{string}' must end with '[pyarrow]'")
2342-
if string == "string[pyarrow]":
2342+
if string in ("string[pyarrow]", "str[pyarrow]"):
23432343
# Ensure Registry.find skips ArrowDtype to use StringDtype instead
23442344
raise TypeError("string[pyarrow] should be constructed by StringDtype")
23452345
if pa_version_under10p1:

pandas/tests/dtypes/test_common.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,26 @@ def test_pandas_dtype_string_dtypes(string_storage):
837837
assert result == pd.StringDtype(string_storage, na_value=pd.NA)
838838

839839

840+
def test_pandas_dtype_string_dtype_alias_with_storage():
841+
with pytest.raises(TypeError):
842+
pandas_dtype("str[python]")
843+
844+
with pytest.raises(TypeError):
845+
pandas_dtype("str[pyarrow]")
846+
847+
result = pandas_dtype("string[python]")
848+
assert result == pd.StringDtype("python", na_value=pd.NA)
849+
850+
if HAS_PYARROW:
851+
result = pandas_dtype("string[pyarrow]")
852+
assert result == pd.StringDtype("pyarrow", na_value=pd.NA)
853+
else:
854+
with pytest.raises(
855+
ImportError, match="required for PyArrow backed StringArray"
856+
):
857+
pandas_dtype("string[pyarrow]")
858+
859+
840860
@td.skip_if_installed("pyarrow")
841861
def test_construct_from_string_without_pyarrow_installed():
842862
# GH 57928

0 commit comments

Comments
 (0)