Skip to content

REF: de-duplicate code for pad/backfill #40306

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
102 changes: 5 additions & 97 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -597,43 +597,12 @@ def pad(ndarray[algos_t] old, ndarray[algos_t] new, limit=None):

@cython.boundscheck(False)
@cython.wraparound(False)
def pad_inplace(algos_t[:] values, uint8_t[:] mask, limit=None):
cdef:
Py_ssize_t i, N
algos_t val
uint8_t prev_mask
int lim, fill_count = 0

N = len(values)

# GH#2778
if N == 0:
return

lim = validate_limit(N, limit)

val = values[0]
prev_mask = mask[0]
for i in range(N):
if mask[i]:
if fill_count >= lim:
continue
fill_count += 1
values[i] = val
mask[i] = prev_mask
else:
fill_count = 0
val = values[i]
prev_mask = mask[i]


@cython.boundscheck(False)
@cython.wraparound(False)
def pad_2d_inplace(algos_t[:, :] values, const uint8_t[:, :] mask, limit=None):
def pad_2d_inplace(algos_t[:, :] values, uint8_t[:, :] mask, limit=None):
cdef:
Py_ssize_t i, j, N, K
algos_t val
int lim, fill_count = 0
uint8_t prev_mask

K, N = (<object>values).shape

Expand All @@ -646,15 +615,18 @@ def pad_2d_inplace(algos_t[:, :] values, const uint8_t[:, :] mask, limit=None):
for j in range(K):
fill_count = 0
val = values[j, 0]
prev_mask = mask[j, 0]
for i in range(N):
if mask[j, i]:
if fill_count >= lim:
continue
fill_count += 1
values[j, i] = val
mask[j, i] = prev_mask
else:
fill_count = 0
val = values[j, i]
prev_mask = mask[j, i]


"""
Expand Down Expand Up @@ -741,70 +713,6 @@ def backfill(ndarray[algos_t] old, ndarray[algos_t] new, limit=None) -> ndarray:
return indexer


@cython.boundscheck(False)
@cython.wraparound(False)
def backfill_inplace(algos_t[:] values, uint8_t[:] mask, limit=None):
cdef:
Py_ssize_t i, N
algos_t val
uint8_t prev_mask
int lim, fill_count = 0

N = len(values)

# GH#2778
if N == 0:
return

lim = validate_limit(N, limit)

val = values[N - 1]
prev_mask = mask[N - 1]
for i in range(N - 1, -1, -1):
if mask[i]:
if fill_count >= lim:
continue
fill_count += 1
values[i] = val
mask[i] = prev_mask
else:
fill_count = 0
val = values[i]
prev_mask = mask[i]


@cython.boundscheck(False)
@cython.wraparound(False)
def backfill_2d_inplace(algos_t[:, :] values,
const uint8_t[:, :] mask,
limit=None):
cdef:
Py_ssize_t i, j, N, K
algos_t val
int lim, fill_count = 0

K, N = (<object>values).shape

# GH#2778
if N == 0:
return

lim = validate_limit(N, limit)

for j in range(K):
fill_count = 0
val = values[j, N - 1]
for i in range(N - 1, -1, -1):
if mask[j, i]:
if fill_count >= lim:
continue
fill_count += 1
values[j, i] = val
else:
fill_count = 0
val = values[j, i]


@cython.boundscheck(False)
@cython.wraparound(False)
def is_monotonic(ndarray[algos_t, ndim=1] arr, bint timelike):
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ def fillna(

if mask.any():
if method is not None:
func = missing.get_fill_func(method)
new_values, _ = func(self._ndarray.copy(), limit=limit, mask=mask)
new_values, _ = missing.interpolate_2d(
self._ndarray.copy(), method=method, limit=limit, mask=mask
)
# TODO: PandasArray didn't used to copy, need tests for this
new_values = self._from_backing_data(new_values)
else:
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,9 @@ def fillna(self, value=None, method=None, limit=None):

if mask.any():
if method is not None:
func = missing.get_fill_func(method)
new_values, _ = func(self.astype(object), limit=limit, mask=mask)
new_values, _ = missing.interpolate_2d(
self.astype(object), method=method, limit=limit, mask=mask
)
new_values = self._from_sequence(new_values, dtype=self.dtype)
else:
# fill with value
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1774,10 +1774,8 @@ def fillna(self, value=None, method=None, limit=None):
# pad / bfill

# TODO: dispatch when self.categories is EA-dtype
values = np.asarray(self).reshape(-1, len(self))
values = interpolate_2d(values, method, 0, None).astype(
self.categories.dtype
)[0]
values, _ = interpolate_2d(np.asarray(self), method=method)
values = values.astype(self.categories.dtype)
codes = _get_codes_for_values(values, self.categories)

else:
Expand Down
16 changes: 4 additions & 12 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
is_string_dtype,
pandas_dtype,
)
from pandas.core.dtypes.inference import is_array_like
from pandas.core.dtypes.missing import (
isna,
notna,
Expand Down Expand Up @@ -156,24 +155,17 @@ def fillna(
value, method = validate_fillna_kwargs(value, method)

mask = self._mask

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 = missing.get_fill_func(method)
new_values, new_mask = func(
new_values, new_mask = missing.interpolate_2d(
self._data.copy(),
method=method,
limit=limit,
mask=mask.copy(),
)
return type(self)(new_values, new_mask.view(np.bool_))
return type(self)(new_values, new_mask)
else:
# fill with value
new_values = self.copy()
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def fillna(self, value=None, method=None, limit=None):
elif method is not None:
msg = "fillna with 'method' requires high memory usage."
warnings.warn(msg, PerformanceWarning)
filled = interpolate_2d(np.asarray(self), method=method, limit=limit)
filled, _ = interpolate_2d(np.asarray(self), method=method, limit=limit)
return type(self)(filled, fill_value=self.fill_value)

else:
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ def fillna(self, value=None, method=None, limit=None):

if mask.any():
if method is not None:
func = missing.get_fill_func(method)
new_values, _ = func(self.to_numpy(object), limit=limit, mask=mask)
new_values, _ = missing.interpolate_2d(
self.to_numpy(object), method=method, limit=limit, mask=mask
)
new_values = self._from_sequence(new_values)
else:
# fill with value
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ def _interpolate_with_fill(

values = self.values if inplace else self.values.copy()

values = missing.interpolate_2d(
values, _ = missing.interpolate_2d(
values,
method=method,
axis=axis,
Expand Down
Loading