Skip to content

Fix: StringArray non-extensible due to inconsisent assertion #34310

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
wants to merge 9 commits into from
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ ExtensionArray
- Fixed bug that caused :meth:`Series.__repr__()` to crash for extension types whose elements are multidimensional arrays (:issue:`33770`).
- Fixed bug where :meth:`Series.update` would raise a ``ValueError`` for ``ExtensionArray`` dtypes with missing values (:issue:`33980`)
- Fixed bug where :meth:`StringArray.memory_usage` was not implemented (:issue:`33963`)
- Fixed bug where extending :meth:`StringArray._from_sequence` would fail (:issue:`34309`)
- Fixed bug where :meth:`DataFrameGroupBy` would ignore the ``min_count`` argument for aggregations on nullable Boolean dtypes (:issue:`34051`)
- Fixed bug where the constructor of :class:`DataFrame` with ``dtype='string'`` would fail (:issue:`27953`, :issue:`33623`)
- Bug where :class:`DataFrame` column set to scalar extension type was considered an object type rather than the extension type (:issue:`34832`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _validate(self):
@classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
if dtype:
assert dtype == "string"
assert isinstance(dtype, StringDtype)
Copy link
Member

Choose a reason for hiding this comment

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

im not sure this is right. cc @TomAugspurger ?

Copy link
Contributor

Choose a reason for hiding this comment

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

They're equivalent right now, but that may be changing in the future with an arrow-backed string array.

But my main objection to this was the original motivation from the issue not being a supported use case.

Copy link
Member

Choose a reason for hiding this comment

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

@TomAugspurger if you dont think this should be supported, should this PR be closed?


# convert non-na-likes to str, and nan-likes to StringDtype.na_value
result = lib.ensure_string_array(
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import operator
from typing import Type

import numpy as np
import pytest

import pandas.util._test_decorators as td

from pandas.core.dtypes.base import register_extension_dtype
from pandas.core.dtypes.common import is_string_dtype

import pandas as pd
import pandas._testing as tm

Expand Down Expand Up @@ -338,6 +342,29 @@ def test_memory_usage():
assert 0 < series.nbytes <= series.memory_usage() < series.memory_usage(deep=True)


def test_string_dtype_subclassing():
@register_extension_dtype
class MyExtensionDtype(pd.StringDtype):
name = "my_extension"

def __repr__(self) -> str:
return "MyExtensionDtype"

@classmethod
def construct_array_type(cls) -> "Type[MyExtensionStringArray]":
return MyExtensionStringArray

class MyExtensionStringArray(pd.core.arrays.StringArray):
def __init__(self, values, copy=False):
super().__init__(values, copy)
self._dtype = MyExtensionDtype()

series = pd.Series(["test", "test2"], dtype="my_extension")
assert series.dtype == "my_extension"
assert series.values == ["test", "test2"]
assert is_string_dtype(series)


@pytest.mark.parametrize("dtype", [np.float16, np.float32, np.float64])
def test_astype_from_float_dtype(dtype):
# https://github.com/pandas-dev/pandas/issues/36451
Expand Down