Skip to content

[WIP] Arrow string array: Common base class #37337

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

Closed
Closed
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
9 changes: 9 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandas.util._decorators import cache_readonly, doc
from pandas.util._validators import validate_fillna_kwargs

from pandas.core.dtypes.base import ExtensionDtype
from pandas.core.dtypes.common import is_dtype_equal
from pandas.core.dtypes.inference import is_array_like
from pandas.core.dtypes.missing import array_equivalent
Expand Down Expand Up @@ -240,3 +241,11 @@ def _reduce(self, name: str, skipna: bool = True, **kwargs):
else:
msg = f"'{type(self).__name__}' does not implement reduction '{name}'"
raise TypeError(msg)


class StringArrayBase(ExtensionArray):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was for consistency with StringDtypeBase below and a first step towards #35169 (comment)

not actually needed for task in hand, i.e. fix astype failures.

pass


class StringDtypeBase(ExtensionDtype):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could alternative live in pandas\core\dtypes\base.py (might also prevent circular imports)

pass
6 changes: 4 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,17 @@ def astype(self, dtype, copy=True):
array : ndarray
NumPy ndarray with 'dtype' for its dtype.
"""
from pandas.core.arrays.string_ import StringDtype
from pandas.core.arrays._mixins import StringDtypeBase

dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, self.dtype):
if not copy:
return self
elif copy:
return self.copy()
if isinstance(dtype, StringDtype): # allow conversion to StringArrays

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

return np.array(self, dtype=dtype, copy=copy)
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pandas._libs import lib, missing as libmissing

from pandas.core.dtypes.base import ExtensionDtype, register_extension_dtype
from pandas.core.dtypes.base import register_extension_dtype
from pandas.core.dtypes.common import (
is_array_like,
is_bool_dtype,
Expand All @@ -21,12 +21,14 @@
from pandas.core.indexers import check_array_indexer
from pandas.core.missing import isna

from pandas.core.arrays._mixins import StringArrayBase, StringDtypeBase

if TYPE_CHECKING:
import pyarrow


@register_extension_dtype
class StringDtype(ExtensionDtype):
class StringDtype(StringDtypeBase):
"""
Extension dtype for string data.

Expand Down Expand Up @@ -100,7 +102,7 @@ def __from_arrow__(
return StringArray._concat_same_type(results)


class StringArray(PandasArray):
class StringArray(StringArrayBase, PandasArray):
"""
Extension array for string data.

Expand Down
Loading