Skip to content

Commit 4149f32

Browse files
BUG: clean_fill_method failing to raise (#53620)
* BUG: clean_fill_method failing to raise * mypy fixup * Update doc/source/whatsnew/v2.1.0.rst Co-authored-by: Matthew Roeschke <[email protected]> * sort note --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 7aed7bd commit 4149f32

File tree

9 files changed

+40
-7
lines changed

9 files changed

+40
-7
lines changed

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,11 @@ Other
506506
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
507507
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)
508508
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)
509+
- Bug in :meth:`Series.align`, :meth:`DataFrame.align`, :meth:`Series.reindex`, :meth:`DataFrame.reindex`, :meth:`Series.interpolate`, :meth:`DataFrame.interpolate`, incorrectly failing to raise with method="asfreq" (:issue:`53620`)
509510
- Bug in :meth:`Series.map` when giving a callable to an empty series, the returned series had ``object`` dtype. It now keeps the original dtype (:issue:`52384`)
510511
- Bug in :meth:`Series.memory_usage` when ``deep=True`` throw an error with Series of objects and the returned value is incorrect, as it does not take into account GC corrections (:issue:`51858`)
511512
- Fixed incorrect ``__name__`` attribute of ``pandas._libs.json`` (:issue:`52898`)
513+
-
512514

513515
.. ***DO NOT USE THIS SECTION***
514516

pandas/core/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9681,7 +9681,9 @@ def align(
96819681
method = None
96829682
if limit is lib.no_default:
96839683
limit = None
9684-
method = clean_fill_method(method)
9684+
9685+
if method is not None:
9686+
method = clean_fill_method(method)
96859687

96869688
if broadcast_axis is not lib.no_default:
96879689
# GH#51856

pandas/core/internals/blocks.py

+6
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,12 @@ def interpolate(
13691369
m = missing.clean_fill_method(method)
13701370
except ValueError:
13711371
m = None
1372+
# error: Non-overlapping equality check (left operand type:
1373+
# "Literal['backfill', 'bfill', 'ffill', 'pad']", right
1374+
# operand type: "Literal['asfreq']")
1375+
if method == "asfreq": # type: ignore[comparison-overlap]
1376+
# clean_fill_method used to allow this
1377+
raise
13721378
if m is None and self.dtype.kind != "f":
13731379
# only deal with floats
13741380
# bc we already checked that can_hold_na, we don't have int dtype here

pandas/core/missing.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ def mask_missing(arr: ArrayLike, values_to_mask) -> npt.NDArray[np.bool_]:
124124
return mask
125125

126126

127-
def clean_fill_method(method: str | None, allow_nearest: bool = False):
128-
# asfreq is compat for resampling
129-
if method in [None, "asfreq"]:
130-
return None
131-
127+
def clean_fill_method(method: str, allow_nearest: bool = False):
132128
if isinstance(method, str):
133129
method = method.lower()
134130
if method == "ffill":
@@ -1006,6 +1002,8 @@ def get_fill_func(method, ndim: int = 1):
10061002

10071003

10081004
def clean_reindex_fill_method(method) -> ReindexMethod | None:
1005+
if method is None:
1006+
return None
10091007
return clean_fill_method(method, allow_nearest=True)
10101008

10111009

pandas/core/resample.py

+4
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,8 @@ def _upsample(self, method, limit: int | None = None, fill_value=None):
15061506
result = obj.copy()
15071507
result.index = res_index
15081508
else:
1509+
if method == "asfreq":
1510+
method = None
15091511
result = obj.reindex(
15101512
res_index, method=method, limit=limit, fill_value=fill_value
15111513
)
@@ -1624,6 +1626,8 @@ def _upsample(self, method, limit: int | None = None, fill_value=None):
16241626
memb = ax.asfreq(self.freq, how=self.convention)
16251627

16261628
# Get the fill indexer
1629+
if method == "asfreq":
1630+
method = None
16271631
indexer = memb.get_indexer(new_index, method=method, limit=limit)
16281632
new_obj = _take_new_index(
16291633
obj,

pandas/tests/copy_view/test_interp_fillna.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_interpolate_cleaned_fill_method(using_copy_on_write):
113113
df = DataFrame({"a": ["a", np.nan, "c"], "b": 1})
114114
df_orig = df.copy()
115115

116-
result = df.interpolate(method="asfreq")
116+
result = df.interpolate(method="linear")
117117

118118
if using_copy_on_write:
119119
assert np.shares_memory(get_array(result, "a"), get_array(df, "a"))

pandas/tests/frame/methods/test_align.py

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515

1616
class TestDataFrameAlign:
17+
def test_align_asfreq_method_raises(self):
18+
df = DataFrame({"A": [1, np.nan, 2]})
19+
msg = "Invalid fill method"
20+
msg2 = "The 'method', 'limit', and 'fill_axis' keywords"
21+
with pytest.raises(ValueError, match=msg):
22+
with tm.assert_produces_warning(FutureWarning, match=msg2):
23+
df.align(df.iloc[::-1], method="asfreq")
24+
1725
def test_frame_align_aware(self):
1826
idx1 = date_range("2001", periods=5, freq="H", tz="US/Eastern")
1927
idx2 = date_range("2001", periods=5, freq="2H", tz="US/Eastern")

pandas/tests/frame/methods/test_reindex.py

+7
Original file line numberDiff line numberDiff line change
@@ -1291,3 +1291,10 @@ def test_reindex_not_category(self, index_df, index_res, index_exp):
12911291
result = df.reindex(index=index_res)
12921292
expected = DataFrame(index=index_exp)
12931293
tm.assert_frame_equal(result, expected)
1294+
1295+
def test_invalid_method(self):
1296+
df = DataFrame({"A": [1, np.nan, 2]})
1297+
1298+
msg = "Invalid fill method"
1299+
with pytest.raises(ValueError, match=msg):
1300+
df.reindex([1, 0, 2], method="asfreq")

pandas/tests/series/methods/test_interpolate.py

+6
Original file line numberDiff line numberDiff line change
@@ -845,3 +845,9 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):
845845
result = ts.sort_index(ascending=ascending).interpolate(method="index")
846846
expected = Series(data=expected_values, index=expected_values, dtype=float)
847847
tm.assert_series_equal(result, expected)
848+
849+
def test_interpolate_afreq_raises(self):
850+
ser = Series(["a", None, "b"], dtype=object)
851+
msg = "Invalid fill method"
852+
with pytest.raises(ValueError, match=msg):
853+
ser.interpolate(method="asfreq")

0 commit comments

Comments
 (0)