Skip to content

Commit 2cea420

Browse files
authored
Fix frame_or_series.asfreq() dropping rows on unordered indices (#40384)
1 parent 2700775 commit 2cea420

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ Groupby/resample/rolling
631631
- Bug in :class:`core.window.ewm.ExponentialMovingWindow` when calling ``__getitem__`` would incorrectly raise a ``ValueError`` when providing ``times`` (:issue:`40164`)
632632
- Bug in :class:`core.window.ewm.ExponentialMovingWindow` when calling ``__getitem__`` would not retain ``com``, ``span``, ``alpha`` or ``halflife`` attributes (:issue:`40164`)
633633
- :class:`core.window.ewm.ExponentialMovingWindow` now raises a ``NotImplementedError`` when specifying ``times`` with ``adjust=False`` due to an incorrect calculation (:issue:`40098`)
634+
- Bug in :meth:`Series.asfreq` and :meth:`DataFrame.asfreq` dropping rows when the index is not sorted (:issue:`39805`)
634635

635636
Reshaping
636637
^^^^^^^^^

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ def asfreq(obj, freq, method=None, how=None, normalize=False, fill_value=None):
19861986

19871987
new_obj.index = _asfreq_compat(obj.index, freq)
19881988
else:
1989-
dti = date_range(obj.index[0], obj.index[-1], freq=freq)
1989+
dti = date_range(obj.index.min(), obj.index.max(), freq=freq)
19901990
dti.name = obj.index.name
19911991
new_obj = obj.reindex(dti, method=method, fill_value=fill_value)
19921992
if normalize:

pandas/tests/frame/methods/test_asfreq.py

+12
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,15 @@ def test_asfreq_with_date_object_index(self, frame_or_series):
9191
result = ts2.asfreq("4H", method="ffill")
9292
expected = ts.asfreq("4H", method="ffill")
9393
tm.assert_equal(result, expected)
94+
95+
def test_asfreq_with_unsorted_index(self, frame_or_series):
96+
# GH#39805
97+
# Test that rows are not dropped when the datetime index is out of order
98+
index = to_datetime(["2021-01-04", "2021-01-02", "2021-01-03", "2021-01-01"])
99+
result = frame_or_series(range(4), index=index)
100+
101+
expected = result.reindex(sorted(index))
102+
expected.index = expected.index._with_freq("infer")
103+
104+
result = result.asfreq("D")
105+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)