Skip to content

CLN/TST: delegate StringArray.fillna() to parent class + add tests #37987

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 19 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 16 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
31 changes: 18 additions & 13 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,8 @@ def _values_for_factorize(self):
arr[mask] = -1
return arr, -1

def __setitem__(self, key, value):
value = extract_array(value, extract_numpy=True)
if isinstance(value, type(self)):
# extract_array doesn't extract PandasArray subclasses
value = value._ndarray

key = check_array_indexer(self, key)
scalar_key = lib.is_scalar(key)
def _validate_setitem_value(self, value):
scalar_value = lib.is_scalar(value)
if scalar_key and not scalar_value:
raise ValueError("setting an array element with a sequence.")

# validate new items
if scalar_value:
if isna(value):
value = StringDtype.na_value
Expand All @@ -279,11 +268,27 @@ def __setitem__(self, key, value):
value = np.asarray(value, dtype=object)
if len(value) and not lib.is_string_array(value, skipna=True):
raise ValueError("Must provide strings.")
return value

def __setitem__(self, key, value):
value = extract_array(value, extract_numpy=True)
if isinstance(value, type(self)):
# extract_array doesn't extract PandasArray subclasses
value = value._ndarray

key = check_array_indexer(self, key)
scalar_key = lib.is_scalar(key)
scalar_value = lib.is_scalar(value)
if scalar_key and not scalar_value:
raise ValueError("setting an array element with a sequence.")

value = self._validate_setitem_value(value)

super().__setitem__(key, value)

def fillna(self, value=None, method=None, limit=None):
# TODO: validate dtype
if value is not None:
value = self._validate_setitem_value(value)
Copy link
Member

Choose a reason for hiding this comment

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

This is actually not needed, as the setitem under the hood of fillna already calls it. So calling it here as well means we validate the value twice.

Copy link
Member Author

Choose a reason for hiding this comment

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

right, reverted all the changes here

return super().fillna(value, method, limit)
Copy link
Member

Choose a reason for hiding this comment

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

does this method need to be overriden here at all?

Copy link
Member Author

Choose a reason for hiding this comment

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

it doesn't

Copy link
Member Author

Choose a reason for hiding this comment

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

pushed that now


def astype(self, dtype, copy=True):
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,24 @@ def test_reduce_missing(skipna, dtype):
assert pd.isna(result)


def test_fillna_args():
# GH 37987

arr = pd.array(["a", pd.NA], dtype="string")

res = arr.fillna(value="b")
expected = pd.array(["a", "b"], dtype="string")
tm.assert_extension_array_equal(res, expected)

res = arr.fillna(value=np.str_("b"))
expected = pd.array(["a", "b"], dtype="string")
tm.assert_extension_array_equal(res, expected)

msg = "Cannot set non-string value '1' into a StringArray."
with pytest.raises(ValueError, match=msg):
arr.fillna(value=1)


@td.skip_if_no("pyarrow", min_version="0.15.0")
def test_arrow_array(dtype):
# protocol added in 0.15.0
Expand Down