Skip to content

Commit 87b1e42

Browse files
authored
Remove fillna from Resampler (#57348)
1 parent 5673cc5 commit 87b1e42

File tree

5 files changed

+4
-215
lines changed

5 files changed

+4
-215
lines changed

doc/redirects.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ generated/pandas.core.resample.Resampler.backfill,../reference/api/pandas.core.r
238238
generated/pandas.core.resample.Resampler.bfill,../reference/api/pandas.core.resample.Resampler.bfill
239239
generated/pandas.core.resample.Resampler.count,../reference/api/pandas.core.resample.Resampler.count
240240
generated/pandas.core.resample.Resampler.ffill,../reference/api/pandas.core.resample.Resampler.ffill
241-
generated/pandas.core.resample.Resampler.fillna,../reference/api/pandas.core.resample.Resampler.fillna
242241
generated/pandas.core.resample.Resampler.first,../reference/api/pandas.core.resample.Resampler.first
243242
generated/pandas.core.resample.Resampler.get_group,../reference/api/pandas.core.resample.Resampler.get_group
244243
generated/pandas.core.resample.Resampler.groups,../reference/api/pandas.core.resample.Resampler.groups

doc/source/reference/resampling.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ Upsampling
3838
Resampler.ffill
3939
Resampler.bfill
4040
Resampler.nearest
41-
Resampler.fillna
4241
Resampler.asfreq
4342
Resampler.interpolate
4443

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ Removal of prior version deprecations/changes
106106
- :func:`read_excel`, :func:`read_json`, :func:`read_html`, and :func:`read_xml` no longer accept raw string or byte representation of the data. That type of data must be wrapped in a :py:class:`StringIO` or :py:class:`BytesIO` (:issue:`53767`)
107107
- All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`)
108108
- Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`)
109-
- Removed :meth:`DataFrame.applymap`, :meth:`Styler.applymap` and :meth:`Styler.applymap_index` (:issue:`52364`)
109+
- Removed ``DataFrame.applymap``, ``Styler.applymap`` and ``Styler.applymap_index`` (:issue:`52364`)
110110
- Removed ``DataFrame.bool`` and ``Series.bool`` (:issue:`51756`)
111111
- Removed ``DataFrame.first`` and ``DataFrame.last`` (:issue:`53710`)
112112
- Removed ``DataFrame.swapaxes`` and ``Series.swapaxes`` (:issue:`51946`)
113113
- Removed ``DataFrameGroupBy.grouper`` and ``SeriesGroupBy.grouper`` (:issue:`56521`)
114114
- Removed ``DataFrameGroupby.fillna`` and ``SeriesGroupBy.fillna``` (:issue:`55719`)
115115
- Removed ``Index.format``, use :meth:`Index.astype` with ``str`` or :meth:`Index.map` with a ``formatter`` function instead (:issue:`55439`)
116+
- Removed ``Resample.fillna`` (:issue:`55719`)
116117
- Removed ``Series.__int__`` and ``Series.__float__``. Call ``int(Series.iloc[0])`` or ``float(Series.iloc[0])`` instead. (:issue:`51131`)
117118
- Removed ``Series.ravel`` (:issue:`56053`)
118119
- Removed ``Series.view`` (:issue:`56054`)

pandas/core/resample.py

Lines changed: 2 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,8 @@ def nearest(self, limit: int | None = None):
631631
632632
See Also
633633
--------
634-
backfill : Backward fill the new missing values in the resampled data.
635-
pad : Forward fill ``NaN`` values.
634+
bfill : Backward fill the new missing values in the resampled data.
635+
ffill : Forward fill ``NaN`` values.
636636
637637
Examples
638638
--------
@@ -686,9 +686,6 @@ def bfill(self, limit: int | None = None):
686686
687687
See Also
688688
--------
689-
bfill : Alias of backfill.
690-
fillna : Fill NaN values using the specified method, which can be
691-
'backfill'.
692689
nearest : Fill NaN values with nearest neighbor starting from center.
693690
ffill : Forward fill NaN values.
694691
Series.fillna : Fill NaN values in the Series using the
@@ -767,177 +764,6 @@ def bfill(self, limit: int | None = None):
767764
"""
768765
return self._upsample("bfill", limit=limit)
769766

770-
@final
771-
def fillna(self, method, limit: int | None = None):
772-
"""
773-
Fill missing values introduced by upsampling.
774-
775-
In statistics, imputation is the process of replacing missing data with
776-
substituted values [1]_. When resampling data, missing values may
777-
appear (e.g., when the resampling frequency is higher than the original
778-
frequency).
779-
780-
Missing values that existed in the original data will
781-
not be modified.
782-
783-
Parameters
784-
----------
785-
method : {'pad', 'backfill', 'ffill', 'bfill', 'nearest'}
786-
Method to use for filling holes in resampled data
787-
788-
* 'pad' or 'ffill': use previous valid observation to fill gap
789-
(forward fill).
790-
* 'backfill' or 'bfill': use next valid observation to fill gap.
791-
* 'nearest': use nearest valid observation to fill gap.
792-
793-
limit : int, optional
794-
Limit of how many consecutive missing values to fill.
795-
796-
Returns
797-
-------
798-
Series or DataFrame
799-
An upsampled Series or DataFrame with missing values filled.
800-
801-
See Also
802-
--------
803-
bfill : Backward fill NaN values in the resampled data.
804-
ffill : Forward fill NaN values in the resampled data.
805-
nearest : Fill NaN values in the resampled data
806-
with nearest neighbor starting from center.
807-
interpolate : Fill NaN values using interpolation.
808-
Series.fillna : Fill NaN values in the Series using the
809-
specified method, which can be 'bfill' and 'ffill'.
810-
DataFrame.fillna : Fill NaN values in the DataFrame using the
811-
specified method, which can be 'bfill' and 'ffill'.
812-
813-
References
814-
----------
815-
.. [1] https://en.wikipedia.org/wiki/Imputation_(statistics)
816-
817-
Examples
818-
--------
819-
Resampling a Series:
820-
821-
>>> s = pd.Series(
822-
... [1, 2, 3], index=pd.date_range("20180101", periods=3, freq="h")
823-
... )
824-
>>> s
825-
2018-01-01 00:00:00 1
826-
2018-01-01 01:00:00 2
827-
2018-01-01 02:00:00 3
828-
Freq: h, dtype: int64
829-
830-
Without filling the missing values you get:
831-
832-
>>> s.resample("30min").asfreq()
833-
2018-01-01 00:00:00 1.0
834-
2018-01-01 00:30:00 NaN
835-
2018-01-01 01:00:00 2.0
836-
2018-01-01 01:30:00 NaN
837-
2018-01-01 02:00:00 3.0
838-
Freq: 30min, dtype: float64
839-
840-
>>> s.resample("30min").fillna("backfill")
841-
2018-01-01 00:00:00 1
842-
2018-01-01 00:30:00 2
843-
2018-01-01 01:00:00 2
844-
2018-01-01 01:30:00 3
845-
2018-01-01 02:00:00 3
846-
Freq: 30min, dtype: int64
847-
848-
>>> s.resample("15min").fillna("backfill", limit=2)
849-
2018-01-01 00:00:00 1.0
850-
2018-01-01 00:15:00 NaN
851-
2018-01-01 00:30:00 2.0
852-
2018-01-01 00:45:00 2.0
853-
2018-01-01 01:00:00 2.0
854-
2018-01-01 01:15:00 NaN
855-
2018-01-01 01:30:00 3.0
856-
2018-01-01 01:45:00 3.0
857-
2018-01-01 02:00:00 3.0
858-
Freq: 15min, dtype: float64
859-
860-
>>> s.resample("30min").fillna("pad")
861-
2018-01-01 00:00:00 1
862-
2018-01-01 00:30:00 1
863-
2018-01-01 01:00:00 2
864-
2018-01-01 01:30:00 2
865-
2018-01-01 02:00:00 3
866-
Freq: 30min, dtype: int64
867-
868-
>>> s.resample("30min").fillna("nearest")
869-
2018-01-01 00:00:00 1
870-
2018-01-01 00:30:00 2
871-
2018-01-01 01:00:00 2
872-
2018-01-01 01:30:00 3
873-
2018-01-01 02:00:00 3
874-
Freq: 30min, dtype: int64
875-
876-
Missing values present before the upsampling are not affected.
877-
878-
>>> sm = pd.Series(
879-
... [1, None, 3], index=pd.date_range("20180101", periods=3, freq="h")
880-
... )
881-
>>> sm
882-
2018-01-01 00:00:00 1.0
883-
2018-01-01 01:00:00 NaN
884-
2018-01-01 02:00:00 3.0
885-
Freq: h, dtype: float64
886-
887-
>>> sm.resample("30min").fillna("backfill")
888-
2018-01-01 00:00:00 1.0
889-
2018-01-01 00:30:00 NaN
890-
2018-01-01 01:00:00 NaN
891-
2018-01-01 01:30:00 3.0
892-
2018-01-01 02:00:00 3.0
893-
Freq: 30min, dtype: float64
894-
895-
>>> sm.resample("30min").fillna("pad")
896-
2018-01-01 00:00:00 1.0
897-
2018-01-01 00:30:00 1.0
898-
2018-01-01 01:00:00 NaN
899-
2018-01-01 01:30:00 NaN
900-
2018-01-01 02:00:00 3.0
901-
Freq: 30min, dtype: float64
902-
903-
>>> sm.resample("30min").fillna("nearest")
904-
2018-01-01 00:00:00 1.0
905-
2018-01-01 00:30:00 NaN
906-
2018-01-01 01:00:00 NaN
907-
2018-01-01 01:30:00 3.0
908-
2018-01-01 02:00:00 3.0
909-
Freq: 30min, dtype: float64
910-
911-
DataFrame resampling is done column-wise. All the same options are
912-
available.
913-
914-
>>> df = pd.DataFrame(
915-
... {"a": [2, np.nan, 6], "b": [1, 3, 5]},
916-
... index=pd.date_range("20180101", periods=3, freq="h"),
917-
... )
918-
>>> df
919-
a b
920-
2018-01-01 00:00:00 2.0 1
921-
2018-01-01 01:00:00 NaN 3
922-
2018-01-01 02:00:00 6.0 5
923-
924-
>>> df.resample("30min").fillna("bfill")
925-
a b
926-
2018-01-01 00:00:00 2.0 1
927-
2018-01-01 00:30:00 NaN 3
928-
2018-01-01 01:00:00 NaN 3
929-
2018-01-01 01:30:00 6.0 5
930-
2018-01-01 02:00:00 6.0 5
931-
"""
932-
warnings.warn(
933-
f"{type(self).__name__}.fillna is deprecated and will be removed "
934-
"in a future version. Use obj.ffill(), obj.bfill(), "
935-
"or obj.nearest() instead.",
936-
FutureWarning,
937-
stacklevel=find_stack_level(),
938-
)
939-
return self._upsample(method, limit=limit)
940-
941767
@final
942768
def interpolate(
943769
self,
@@ -1808,11 +1634,6 @@ def _upsample(self, method, limit: int | None = None, fill_value=None):
18081634
Maximum size gap to fill when reindexing
18091635
fill_value : scalar, default None
18101636
Value to use for missing values
1811-
1812-
See Also
1813-
--------
1814-
.fillna: Fill NA/NaN values using the specified method.
1815-
18161637
"""
18171638
if self._from_selection:
18181639
raise ValueError(
@@ -1961,11 +1782,6 @@ def _upsample(self, method, limit: int | None = None, fill_value=None):
19611782
Maximum size gap to fill when reindexing.
19621783
fill_value : scalar, default None
19631784
Value to use for missing values.
1964-
1965-
See Also
1966-
--------
1967-
.fillna: Fill NA/NaN values using the specified method.
1968-
19691785
"""
19701786
# we may need to actually resample as if we are timestamps
19711787
if self.kind == "timestamp":

pandas/tests/resample/test_resample_api.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -296,32 +296,6 @@ def test_transform_frame(on):
296296
tm.assert_frame_equal(result, expected)
297297

298298

299-
def test_fillna():
300-
# need to upsample here
301-
rng = date_range("1/1/2012", periods=10, freq="2s")
302-
ts = Series(np.arange(len(rng), dtype="int64"), index=rng)
303-
r = ts.resample("s")
304-
305-
expected = r.ffill()
306-
msg = "DatetimeIndexResampler.fillna is deprecated"
307-
with tm.assert_produces_warning(FutureWarning, match=msg):
308-
result = r.fillna(method="ffill")
309-
tm.assert_series_equal(result, expected)
310-
311-
expected = r.bfill()
312-
with tm.assert_produces_warning(FutureWarning, match=msg):
313-
result = r.fillna(method="bfill")
314-
tm.assert_series_equal(result, expected)
315-
316-
msg2 = (
317-
r"Invalid fill method\. Expecting pad \(ffill\), backfill "
318-
r"\(bfill\) or nearest\. Got 0"
319-
)
320-
with pytest.raises(ValueError, match=msg2):
321-
with tm.assert_produces_warning(FutureWarning, match=msg):
322-
r.fillna(0)
323-
324-
325299
@pytest.mark.parametrize(
326300
"func",
327301
[

0 commit comments

Comments
 (0)