Skip to content

Commit 5466d8c

Browse files
committed
Moved logic for calling missing.interpolate_1d_fill to missing.interpolate_2d
1 parent a467afd commit 5466d8c

File tree

2 files changed

+60
-53
lines changed

2 files changed

+60
-53
lines changed

pandas/core/internals/blocks.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,34 +1170,15 @@ def _interpolate_with_fill(
11701170
# We only get here for non-ExtensionBlock
11711171
fill_value = convert_scalar(self.values, fill_value)
11721172

1173-
# We have to distinguish two cases:
1174-
# 1. When kwarg `limit_area` is used: It is not
1175-
# supported by `missing.interpolate_2d()`. Using this kwarg only
1176-
# works by applying the fill along a certain axis.
1177-
# 2. All other cases: Then, `missing.interpolate_2d()` can be used.
1178-
if limit_area is not None:
1179-
1180-
def func(x):
1181-
return missing.interpolate_1d_fill(
1182-
x,
1183-
method=method,
1184-
limit=limit,
1185-
limit_area=limit_area,
1186-
fill_value=fill_value,
1187-
dtype=self.dtype,
1188-
)
1189-
1190-
# Beware that this also changes the input array `values`!
1191-
interp_values = np.apply_along_axis(func, axis, values)
1192-
else:
1193-
interp_values = missing.interpolate_2d(
1194-
values,
1195-
method=method,
1196-
axis=axis,
1197-
limit=limit,
1198-
fill_value=fill_value,
1199-
dtype=self.dtype,
1200-
)
1173+
interp_values = missing.interpolate_2d(
1174+
values,
1175+
method=method,
1176+
axis=axis,
1177+
limit=limit,
1178+
fill_value=fill_value,
1179+
limit_area=limit_area,
1180+
dtype=self.dtype,
1181+
)
12011182

12021183
blocks = [self.make_block_same_class(interp_values, ndim=self.ndim)]
12031184
return self._maybe_downcast(blocks, downcast)

pandas/core/missing.py

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -577,43 +577,69 @@ def interpolate_1d_fill(
577577

578578

579579
def interpolate_2d(
580-
values, method="pad", axis=0, limit=None, fill_value=None, dtype=None
580+
values,
581+
method="pad",
582+
axis=0,
583+
limit=None,
584+
fill_value=None,
585+
limit_area=None,
586+
dtype=None,
581587
):
582588
"""
583589
Perform an actual interpolation of values, values will be make 2-d if
584590
needed fills inplace, returns the result.
585591
"""
586592
orig_values = values
587593

588-
transf = (lambda x: x) if axis == 0 else (lambda x: x.T)
589-
590-
# reshape a 1 dim if needed
591-
ndim = values.ndim
592-
if values.ndim == 1:
593-
if axis != 0: # pragma: no cover
594-
raise AssertionError("cannot interpolate on a ndim == 1 with axis != 0")
595-
values = values.reshape(tuple((1,) + values.shape))
594+
# We have to distinguish two cases:
595+
# 1. When kwarg `limit_area` is used: It is not
596+
# supported by `pad_2d` and `backfill_2d`. Using this kwarg only
597+
# works by applying the fill along a certain axis.
598+
# 2. All other cases.
599+
if limit_area is not None:
596600

597-
if fill_value is None:
598-
mask = None
599-
else: # todo create faster fill func without masking
600-
mask = mask_missing(transf(values), fill_value)
601+
def func(x):
602+
return interpolate_1d_fill(
603+
x,
604+
method=method,
605+
limit=limit,
606+
limit_area=limit_area,
607+
fill_value=fill_value,
608+
dtype=dtype,
609+
)
601610

602-
method = clean_fill_method(method)
603-
if method == "pad":
604-
values = transf(pad_2d(transf(values), limit=limit, mask=mask, dtype=dtype))
611+
# Beware that this also changes the input array `values`!
612+
values = np.apply_along_axis(func, axis, values)
605613
else:
606-
values = transf(
607-
backfill_2d(transf(values), limit=limit, mask=mask, dtype=dtype)
608-
)
614+
transf = (lambda x: x) if axis == 0 else (lambda x: x.T)
615+
616+
# reshape a 1 dim if needed
617+
ndim = values.ndim
618+
if values.ndim == 1:
619+
if axis != 0: # pragma: no cover
620+
raise AssertionError("cannot interpolate on a ndim == 1 with axis != 0")
621+
values = values.reshape(tuple((1,) + values.shape))
622+
623+
if fill_value is None:
624+
mask = None
625+
else: # todo create faster fill func without masking
626+
mask = mask_missing(transf(values), fill_value)
627+
628+
method = clean_fill_method(method)
629+
if method == "pad":
630+
values = transf(pad_2d(transf(values), limit=limit, mask=mask, dtype=dtype))
631+
else:
632+
values = transf(
633+
backfill_2d(transf(values), limit=limit, mask=mask, dtype=dtype)
634+
)
609635

610-
# reshape back
611-
if ndim == 1:
612-
values = values[0]
636+
# reshape back
637+
if ndim == 1:
638+
values = values[0]
613639

614-
if orig_values.dtype.kind == "M":
615-
# convert float back to datetime64
616-
values = values.astype(orig_values.dtype)
640+
if orig_values.dtype.kind == "M":
641+
# convert float back to datetime64
642+
values = values.astype(orig_values.dtype)
617643

618644
return values
619645

0 commit comments

Comments
 (0)