Skip to content

[ArrowStringDtype] Make it already a StringDtype subclass #41312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ def astype(self, dtype, copy=True):
NumPy ndarray with 'dtype' for its dtype.
"""
from pandas.core.arrays.string_ import StringDtype
from pandas.core.arrays.string_arrow import ArrowStringDtype

dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, self.dtype):
Expand All @@ -540,9 +539,8 @@ def astype(self, dtype, copy=True):
return self.copy()

# FIXME: Really hard-code here?
if isinstance(
dtype, (ArrowStringDtype, StringDtype)
): # allow conversion to StringArrays
if isinstance(dtype, StringDtype):
# allow conversion to StringArrays
return dtype.construct_array_type()._from_sequence(self, copy=False)

return np.array(self, dtype=dtype, copy=copy)
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,6 @@ def astype(self, dtype, copy: bool = True):
"""
from pandas import Index
from pandas.core.arrays.string_ import StringDtype
from pandas.core.arrays.string_arrow import ArrowStringDtype

if dtype is not None:
dtype = pandas_dtype(dtype)
Expand All @@ -852,7 +851,7 @@ def astype(self, dtype, copy: bool = True):
return self._shallow_copy(new_left, new_right)
elif is_categorical_dtype(dtype):
return Categorical(np.asarray(self), dtype=dtype)
elif isinstance(dtype, (StringDtype, ArrowStringDtype)):
elif isinstance(dtype, StringDtype):
return dtype.construct_array_type()._from_sequence(self, copy=False)

# TODO: This try/except will be repeated.
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from pandas.util._decorators import doc
from pandas.util._validators import validate_fillna_kwargs

from pandas.core.dtypes.base import ExtensionDtype
from pandas.core.dtypes.common import (
is_array_like,
is_bool_dtype,
Expand All @@ -42,6 +41,7 @@
from pandas.core.arrays.base import ExtensionArray
from pandas.core.arrays.boolean import BooleanDtype
from pandas.core.arrays.integer import Int64Dtype
from pandas.core.arrays.string_ import StringDtype
from pandas.core.indexers import (
check_array_indexer,
validate_indices,
Expand Down Expand Up @@ -74,7 +74,7 @@


@register_extension_dtype
class ArrowStringDtype(ExtensionDtype):
class ArrowStringDtype(StringDtype):
"""
Extension dtype for string data in a ``pyarrow.ChunkedArray``.

Expand Down Expand Up @@ -110,7 +110,7 @@ def type(self) -> type[str]:
return str

@classmethod
def construct_array_type(cls) -> type_t[ArrowStringArray]:
def construct_array_type(cls) -> type_t[ArrowStringArray]: # type: ignore[override]
"""
Return the array type associated with this dtype.

Expand All @@ -126,7 +126,9 @@ def __hash__(self) -> int:
def __repr__(self) -> str:
return "ArrowStringDtype"

def __from_arrow__(self, array: pa.Array | pa.ChunkedArray) -> ArrowStringArray:
def __from_arrow__( # type: ignore[override]
self, array: pa.Array | pa.ChunkedArray
) -> ArrowStringArray:
"""
Construct StringArray from pyarrow Array/ChunkedArray.
"""
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ class StringMethods(NoNewAttributesMixin):

def __init__(self, data):
from pandas.core.arrays.string_ import StringDtype
from pandas.core.arrays.string_arrow import ArrowStringDtype

self._inferred_dtype = self._validate(data)
self._is_categorical = is_categorical_dtype(data.dtype)
self._is_string = isinstance(data.dtype, (StringDtype, ArrowStringDtype))
self._is_string = isinstance(data.dtype, StringDtype)
self._data = data

self._index = self._name = None
Expand Down Expand Up @@ -3028,9 +3027,8 @@ def _result_dtype(arr):
# ideally we just pass `dtype=arr.dtype` unconditionally, but this fails
# when the list of values is empty.
from pandas.core.arrays.string_ import StringDtype
from pandas.core.arrays.string_arrow import ArrowStringDtype

if isinstance(arr.dtype, (StringDtype, ArrowStringDtype)):
if isinstance(arr.dtype, StringDtype):
return arr.dtype.name
else:
return object
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
ExtensionDtype,
)
from pandas.api.types import is_bool_dtype
from pandas.core.arrays.string_arrow import ArrowStringDtype


class JSONDtype(ExtensionDtype):
Expand Down Expand Up @@ -196,7 +195,7 @@ def astype(self, dtype, copy=True):
if copy:
return self.copy()
return self
elif isinstance(dtype, (StringDtype, ArrowStringDtype)):
elif isinstance(dtype, StringDtype):
value = self.astype(str) # numpy doesn'y like nested dicts
return dtype.construct_array_type()._from_sequence(value, copy=False)

Expand Down