Skip to content

Commit

Permalink
[backport 2.3.x] String dtype: disallow specifying the 'str' dtype wi…
Browse files Browse the repository at this point in the history
…th storage in [..] in string alias (#60661) (#60715)

(cherry picked from commit 7415aca)
  • Loading branch information
jorisvandenbossche authored Jan 14, 2025
1 parent 7374d09 commit fcc94eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@ def construct_from_string(cls, string: str) -> ArrowDtype:
)
if not string.endswith("[pyarrow]"):
raise TypeError(f"'{string}' must end with '[pyarrow]'")
if string == "string[pyarrow]":
if string in ("string[pyarrow]", "str[pyarrow]"):
# Ensure Registry.find skips ArrowDtype to use StringDtype instead
raise TypeError("string[pyarrow] should be constructed by StringDtype")

Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,23 @@ def test_pandas_dtype_string_dtypes(string_storage):
with pd.option_context("string_storage", string_storage):
result = pandas_dtype("string")
assert result == pd.StringDtype(string_storage, na_value=pd.NA)


def test_pandas_dtype_string_dtype_alias_with_storage():
with pytest.raises(TypeError, match="not understood"):
pandas_dtype("str[python]")

with pytest.raises(TypeError, match="not understood"):
pandas_dtype("str[pyarrow]")

result = pandas_dtype("string[python]")
assert result == pd.StringDtype("python", na_value=pd.NA)

if HAS_PYARROW:
result = pandas_dtype("string[pyarrow]")
assert result == pd.StringDtype("pyarrow", na_value=pd.NA)
else:
with pytest.raises(
ImportError, match="required for PyArrow backed StringArray"
):
pandas_dtype("string[pyarrow]")

0 comments on commit fcc94eb

Please sign in to comment.