Skip to content
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
11 changes: 1 addition & 10 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pandas.util._validators import validate_fillna_kwargs

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

from pandas.core import missing
Expand Down Expand Up @@ -275,15 +274,7 @@ def fillna(
value, method = validate_fillna_kwargs(value, method)

mask = self.isna()

# TODO: share this with EA base class implementation
if is_array_like(value):
if len(value) != len(self):
raise ValueError(
f"Length of 'value' does not match. Got ({len(value)}) "
f" expected {len(self)}"
)
value = value[mask]
value = missing.check_value_size(value, mask, len(self))

if mask.any():
if method is not None:
Expand Down
18 changes: 6 additions & 12 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

from pandas.core.dtypes.cast import maybe_cast_to_extension_array
from pandas.core.dtypes.common import (
is_array_like,
is_dtype_equal,
is_list_like,
is_scalar,
Expand All @@ -58,13 +57,15 @@
)
from pandas.core.dtypes.missing import isna

from pandas.core import ops
from pandas.core import (
missing,
ops,
)
from pandas.core.algorithms import (
factorize_array,
isin,
unique,
)
from pandas.core.missing import get_fill_func
from pandas.core.sorting import (
nargminmax,
nargsort,
Expand Down Expand Up @@ -696,18 +697,11 @@ def fillna(self, value=None, method=None, limit=None):
value, method = validate_fillna_kwargs(value, method)

mask = self.isna()

if is_array_like(value):
if len(value) != len(self):
raise ValueError(
f"Length of 'value' does not match. Got ({len(value)}) "
f"expected {len(self)}"
)
value = value[mask]
value = missing.check_value_size(value, mask, len(self))

if mask.any():
if method is not None:
func = get_fill_func(method)
func = missing.get_fill_func(method)
new_values = func(self.astype(object), limit=limit, mask=mask)
new_values = self._from_sequence(new_values, dtype=self.dtype)
else:
Expand Down
13 changes: 3 additions & 10 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
is_integer_dtype,
is_scalar,
)
from pandas.core import missing
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays.base import ExtensionArray
from pandas.core.indexers import (
check_array_indexer,
validate_indices,
)
from pandas.core.missing import get_fill_func

try:
import pyarrow as pa
Expand Down Expand Up @@ -380,18 +380,11 @@ def fillna(self, value=None, method=None, limit=None):
value, method = validate_fillna_kwargs(value, method)

mask = self.isna()

if is_array_like(value):
if len(value) != len(self):
raise ValueError(
f"Length of 'value' does not match. Got ({len(value)}) "
f"expected {len(self)}"
)
value = value[mask]
value = missing.check_value_size(value, mask, len(self))

if mask.any():
if method is not None:
func = get_fill_func(method)
func = missing.get_fill_func(method)
new_values = func(self.to_numpy(object), limit=limit, mask=mask)
new_values = self._from_sequence(new_values)
else:
Expand Down
16 changes: 16 additions & 0 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from pandas.core.dtypes.cast import infer_dtype_from
from pandas.core.dtypes.common import (
ensure_float64,
is_array_like,
is_integer_dtype,
is_numeric_v_string_like,
needs_i8_conversion,
Expand All @@ -39,6 +40,21 @@
from pandas import Index


def check_value_size(value, mask: np.ndarray, length: int):
Copy link
Member

Choose a reason for hiding this comment

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

as this is also masking the values, a different name may be more appropriate.

or maybe return none. just do the validation here and just repeat the masking in the array functions, so it it is clear to readers what is going on.

Copy link
Member Author

Choose a reason for hiding this comment

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

just do the validation here and just repeat the masking in the array functions, so it it is clear to readers what is going on.

thats not a bad idea. i think we do something similar in array_algos.putmask

"""
Validate the size of the values passed to ExtensionArray.fillna.
"""
if is_array_like(value):
if len(value) != length:
raise ValueError(
f"Length of 'value' does not match. Got ({len(value)}) "
f" expected {length}"
)
value = value[mask]

return value


def mask_missing(arr: ArrayLike, values_to_mask) -> np.ndarray:
"""
Return a masking array of same size/shape as arr
Expand Down