Skip to content

Commit 2461315

Browse files
Deprecate method in fillna API (#14278)
This PR deprecates `method` parameter in all public `fillna` APIs to match pandas: pandas-dev/pandas#53496 This PR: ``` = 1056 failed, 99098 passed, 2069 skipped, 776 xfailed, 312 xpassed, 20 errors in 670.87s (0:11:10) = ``` On `pandas_2.0_feature_branch`: ``` = 1584 failed, 98570 passed, 2069 skipped, 776 xfailed, 312 xpassed, 20 errors in 737.24s (0:12:17) = ```
1 parent 7c6d8f2 commit 2461315

File tree

8 files changed

+56
-14
lines changed

8 files changed

+56
-14
lines changed

python/cudf/cudf/core/dataframe.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7031,7 +7031,9 @@ def pct_change(
70317031
"'bfill', or 'backfill'."
70327032
)
70337033

7034-
data = self.fillna(method=fill_method, limit=limit)
7034+
with warnings.catch_warnings():
7035+
warnings.simplefilter("ignore")
7036+
data = self.fillna(method=fill_method, limit=limit)
70357037

70367038
return data.diff(periods=periods) / data.shift(
70377039
periods=periods, freq=freq

python/cudf/cudf/core/frame.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,15 @@ def fillna(
735735
are filled with values in corresponding indices.
736736
A dict can be used to provide different values to fill nulls
737737
in different columns. Cannot be used with ``method``.
738-
739738
method : {'ffill', 'bfill'}, default None
740739
Method to use for filling null values in the dataframe or series.
741740
`ffill` propagates the last non-null values forward to the next
742741
non-null value. `bfill` propagates backward with the next non-null
743742
value. Cannot be used with ``value``.
744743
744+
.. deprecated:: 23.12
745+
`method` is deprecated.
746+
745747
Returns
746748
-------
747749
result : DataFrame, Series, or Index

python/cudf/cudf/core/groupby/groupby.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,14 @@ def fillna(
21832183
if method is not None:
21842184
if method not in {"ffill", "bfill"}:
21852185
raise ValueError("Method can only be of 'ffill', 'bfill'.")
2186+
# Do not remove until pandas 3.0 support is added.
2187+
warnings.warn(
2188+
f"{type(self).__name__}.fillna with 'method' is "
2189+
"deprecated and will raise in a future version. "
2190+
"Use obj.ffill() or obj.bfill() instead.",
2191+
FutureWarning,
2192+
)
2193+
21862194
return getattr(self, method, limit)()
21872195

21882196
values = self.obj.__class__._from_data(
@@ -2295,7 +2303,10 @@ def pct_change(
22952303
FutureWarning,
22962304
)
22972305

2298-
filled = self.fillna(method=fill_method, limit=limit)
2306+
with warnings.catch_warnings():
2307+
warnings.simplefilter("ignore")
2308+
filled = self.fillna(method=fill_method, limit=limit)
2309+
22992310
fill_grp = filled.groupby(self.grouping)
23002311
shifted = fill_grp.shift(periods=periods, freq=freq)
23012312
return (filled / shifted) - 1

python/cudf/cudf/core/indexed_frame.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,14 @@ def _split(self, splits, keep_index=True):
21482148
def fillna(
21492149
self, value=None, method=None, axis=None, inplace=False, limit=None
21502150
): # noqa: D102
2151+
if method is not None:
2152+
# Do not remove until pandas 3.0 support is added.
2153+
warnings.warn(
2154+
f"{type(self).__name__}.fillna with 'method' is "
2155+
"deprecated and will raise in a future version. "
2156+
"Use obj.ffill() or obj.bfill() instead.",
2157+
FutureWarning,
2158+
)
21512159
old_index = self._index
21522160
ret = super().fillna(value, method, axis, inplace, limit)
21532161
if inplace:

python/cudf/cudf/core/resample.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
import pickle
18+
import warnings
1819

1920
import numpy as np
2021
import pandas as pd
@@ -73,7 +74,9 @@ def _scan_fill(self, method: str, limit: int) -> DataFrameOrSeries:
7374
)
7475

7576
# fill the gaps:
76-
filled = upsampled.fillna(method=method)
77+
with warnings.catch_warnings():
78+
warnings.simplefilter("ignore")
79+
filled = upsampled.fillna(method=method)
7780

7881
# filter the result to only include the values corresponding
7982
# to the bin labels:

python/cudf/cudf/core/series.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,9 @@ def pct_change(
35063506
"'bfill', or 'backfill'."
35073507
)
35083508

3509-
data = self.fillna(method=fill_method, limit=limit)
3509+
with warnings.catch_warnings():
3510+
warnings.simplefilter("ignore")
3511+
data = self.fillna(method=fill_method, limit=limit)
35103512
diff = data.diff(periods=periods)
35113513
change = diff / data.shift(periods=periods, freq=freq)
35123514
return change

python/cudf/cudf/tests/test_groupby.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,10 +2646,12 @@ def test_groupby_various_by_fillna(by, data, args):
26462646
ps = pd.Series(data)
26472647
gs = cudf.from_pandas(ps)
26482648

2649-
expect = ps.groupby(by).fillna(**args)
2649+
with expect_warning_if(PANDAS_GE_210 and "method" in args):
2650+
expect = ps.groupby(by).fillna(**args)
26502651
if isinstance(by, pd.Grouper):
26512652
by = cudf.Grouper(level=by.level)
2652-
got = gs.groupby(by).fillna(**args)
2653+
with expect_warning_if("method" in args):
2654+
got = gs.groupby(by).fillna(**args)
26532655

26542656
assert_groupby_results_equal(expect, got, check_dtype=False)
26552657

@@ -2693,8 +2695,10 @@ def test_groupby_fillna_method(nelem, method):
26932695
pdf = t.to_pandas()
26942696
gdf = cudf.from_pandas(pdf)
26952697

2696-
expect = pdf.groupby(key_col).fillna(method=method)
2697-
got = gdf.groupby(key_col).fillna(method=method)
2698+
with expect_warning_if(PANDAS_GE_210):
2699+
expect = pdf.groupby(key_col).fillna(method=method)
2700+
with pytest.warns(FutureWarning):
2701+
got = gdf.groupby(key_col).fillna(method=method)
26982702

26992703
assert_groupby_results_equal(
27002704
expect[value_cols], got[value_cols], sort=False

python/cudf/cudf/tests/test_replace.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
import pytest
99

1010
import cudf
11-
from cudf.core._compat import PANDAS_GE_134, PANDAS_GE_150, PANDAS_GE_200
11+
from cudf.core._compat import (
12+
PANDAS_GE_134,
13+
PANDAS_GE_150,
14+
PANDAS_GE_200,
15+
PANDAS_GE_210,
16+
)
1217
from cudf.core.dtypes import Decimal32Dtype, Decimal64Dtype, Decimal128Dtype
1318
from cudf.testing._utils import (
1419
INTEGER_TYPES,
1520
NUMERIC_TYPES,
1621
assert_eq,
1722
assert_exceptions_equal,
23+
expect_warning_if,
1824
)
1925

2026

@@ -348,8 +354,10 @@ def test_fillna_method_numerical(data, container, data_dtype, method, inplace):
348354
# Explicitly using nans_as_nulls=True
349355
gdata = cudf.from_pandas(pdata, nan_as_null=True)
350356

351-
expected = pdata.fillna(method=method, inplace=inplace)
352-
actual = gdata.fillna(method=method, inplace=inplace)
357+
with expect_warning_if(PANDAS_GE_210):
358+
expected = pdata.fillna(method=method, inplace=inplace)
359+
with pytest.warns(FutureWarning):
360+
actual = gdata.fillna(method=method, inplace=inplace)
353361

354362
if inplace:
355363
expected = pdata
@@ -665,8 +673,10 @@ def test_fillna_method_fixed_width_non_num(data, container, method, inplace):
665673
# Explicitly using nans_as_nulls=True
666674
gdata = cudf.from_pandas(pdata, nan_as_null=True)
667675

668-
expected = pdata.fillna(method=method, inplace=inplace)
669-
actual = gdata.fillna(method=method, inplace=inplace)
676+
with expect_warning_if(PANDAS_GE_210):
677+
expected = pdata.fillna(method=method, inplace=inplace)
678+
with pytest.warns(FutureWarning):
679+
actual = gdata.fillna(method=method, inplace=inplace)
670680

671681
if inplace:
672682
expected = pdata

0 commit comments

Comments
 (0)