Skip to content

Commit 7047d61

Browse files
ENH: Support ffill/bfill on IntervalArray (pandas-dev#54247) (#28)
* ENH: Support ffill/bfill on IntervalArray * gh ref Co-authored-by: jbrockmendel <[email protected]>
1 parent 340d162 commit 7047d61

File tree

5 files changed

+4
-56
lines changed

5 files changed

+4
-56
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ to ``na_action=None``, like for all the other array types.
132132

133133
Other enhancements
134134
^^^^^^^^^^^^^^^^^^
135+
- :meth:`Series.ffill` and :meth:`Series.bfill` are now supported for objects with :class:`IntervalDtype` (:issue:`54247`)
135136
- :meth:`Categorical.map` and :meth:`CategoricalIndex.map` now have a ``na_action`` parameter.
136137
:meth:`Categorical.map` implicitly had a default value of ``"ignore"`` for ``na_action``. This has formally been deprecated and will be changed to ``None`` in the future.
137138
Also notice that :meth:`Series.map` has default ``na_action=None`` and calls to series with categorical data will now use ``na_action=None`` unless explicitly set otherwise (:issue:`44279`)

pandas/core/arrays/interval.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -917,9 +917,7 @@ def fillna(self, value=None, method=None, limit: int | None = None) -> Self:
917917
filled : IntervalArray with NA/NaN filled
918918
"""
919919
if method is not None:
920-
raise TypeError("Filling by method is not supported for IntervalArray.")
921-
if limit is not None:
922-
raise TypeError("limit is not supported for IntervalArray.")
920+
return super().fillna(value=value, method=method, limit=limit)
923921

924922
value_left, value_right = self._validate_scalar(value)
925923

pandas/core/arrays/sparse/array.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
check_array_indexer,
7979
unpack_tuple_and_ellipses,
8080
)
81-
from pandas.core.missing import pad_or_backfill_inplace
8281
from pandas.core.nanops import check_below_min_count
8382

8483
from pandas.io.formats import printing
@@ -757,21 +756,7 @@ def fillna(
757756
raise ValueError("Must specify one of 'method' or 'value'.")
758757

759758
if method is not None:
760-
msg = "fillna with 'method' requires high memory usage."
761-
warnings.warn(
762-
msg,
763-
PerformanceWarning,
764-
stacklevel=find_stack_level(),
765-
)
766-
new_values = np.asarray(self)
767-
# pad_or_backfill_inplace modifies new_values inplace
768-
# error: Argument "method" to "pad_or_backfill_inplace" has incompatible
769-
# type "Literal['backfill', 'bfill', 'ffill', 'pad']"; expected
770-
# "Literal['pad', 'backfill']"
771-
pad_or_backfill_inplace(
772-
new_values, method=method, limit=limit # type: ignore[arg-type]
773-
)
774-
return type(self)(new_values, fill_value=self.fill_value)
759+
return super().fillna(method=method, limit=limit)
775760

776761
else:
777762
new_values = np.where(isna(self.sp_values), value, self.sp_values)

pandas/tests/extension/test_interval.py

-26
Original file line numberDiff line numberDiff line change
@@ -134,32 +134,6 @@ def test_fillna_length_mismatch(self, data_missing):
134134

135135

136136
class TestMissing(BaseInterval, base.BaseMissingTests):
137-
# Index.fillna only accepts scalar `value`, so we have to xfail all
138-
# non-scalar fill tests.
139-
unsupported_fill = pytest.mark.xfail(
140-
reason="Unsupported fillna option for Interval."
141-
)
142-
143-
@unsupported_fill
144-
def test_fillna_limit_pad(self):
145-
super().test_fillna_limit_pad()
146-
147-
@unsupported_fill
148-
def test_fillna_series_method(self):
149-
super().test_fillna_series_method()
150-
151-
@unsupported_fill
152-
def test_fillna_limit_backfill(self):
153-
super().test_fillna_limit_backfill()
154-
155-
@unsupported_fill
156-
def test_fillna_no_op_returns_copy(self):
157-
super().test_fillna_no_op_returns_copy()
158-
159-
@unsupported_fill
160-
def test_fillna_series(self):
161-
super().test_fillna_series()
162-
163137
def test_fillna_non_scalar_raises(self, data_missing):
164138
msg = "can only insert Interval objects and NA into an IntervalArray"
165139
with pytest.raises(TypeError, match=msg):

pandas/tests/extension/test_sparse.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ def test_isna(self, data_missing):
223223
expected = SparseArray([False, False], fill_value=False, dtype=expected_dtype)
224224
self.assert_equal(sarr.isna(), expected)
225225

226-
def test_fillna_limit_pad(self, data_missing):
227-
warns = (PerformanceWarning, FutureWarning)
228-
with tm.assert_produces_warning(warns, check_stacklevel=False):
229-
super().test_fillna_limit_pad(data_missing)
230-
231226
def test_fillna_limit_backfill(self, data_missing):
232227
warns = (PerformanceWarning, FutureWarning)
233228
with tm.assert_produces_warning(warns, check_stacklevel=False):
@@ -238,12 +233,7 @@ def test_fillna_no_op_returns_copy(self, data, request):
238233
request.node.add_marker(
239234
pytest.mark.xfail(reason="returns array with different fill value")
240235
)
241-
with tm.assert_produces_warning(PerformanceWarning, check_stacklevel=False):
242-
super().test_fillna_no_op_returns_copy(data)
243-
244-
def test_fillna_series_method(self, data_missing, fillna_method):
245-
with tm.assert_produces_warning(PerformanceWarning, check_stacklevel=False):
246-
super().test_fillna_series_method(data_missing, fillna_method)
236+
super().test_fillna_no_op_returns_copy(data)
247237

248238
@pytest.mark.xfail(reason="Unsupported")
249239
def test_fillna_series(self):

0 commit comments

Comments
 (0)