Skip to content

Commit a3c208d

Browse files
authored
BUG: resample with asfreq ignores origin if the dataframe has a fixed frequency #62725 (#62763)
1 parent f918172 commit a3c208d

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,7 @@ Groupby/resample/rolling
11701170
- Bug in :meth:`.Resampler.interpolate` on a :class:`DataFrame` with non-uniform sampling and/or indices not aligning with the resulting resampled index would result in wrong interpolation (:issue:`21351`)
11711171
- Bug in :meth:`.Series.rolling` when used with a :class:`.BaseIndexer` subclass and computing min/max (:issue:`46726`)
11721172
- Bug in :meth:`DataFrame.ewm` and :meth:`Series.ewm` when passed ``times`` and aggregation functions other than mean (:issue:`51695`)
1173+
- Bug in :meth:`DataFrame.resample.asfreq` where fixed-frequency indexes with ``origin`` ignored alignment and returned incorrect values. Now ``origin`` and ``offset`` are respected. (:issue:`62725`)
11731174
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` were not keeping the index name when the index had :class:`ArrowDtype` timestamp dtype (:issue:`61222`)
11741175
- Bug in :meth:`DataFrame.resample` changing index type to :class:`MultiIndex` when the dataframe is empty and using an upsample method (:issue:`55572`)
11751176
- Bug in :meth:`DataFrameGroupBy.agg` and :meth:`SeriesGroupBy.agg` that was returning numpy dtype values when input values are pyarrow dtype values, instead of returning pyarrow dtype values. (:issue:`53030`)

pandas/core/resample.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,11 +2136,12 @@ def _upsample(self, method, limit: int | None = None, fill_value=None):
21362136
binner = self.binner
21372137
res_index = self._adjust_binner_for_upsample(binner)
21382138

2139-
# if we have the same frequency as our axis, then we are equal sampling
2139+
# if index exactly matches target grid (same freq & alignment), use fast path
21402140
if (
21412141
limit is None
21422142
and to_offset(ax.inferred_freq) == self.freq
21432143
and len(obj) == len(res_index)
2144+
and obj.index.equals(res_index)
21442145
):
21452146
result = obj.copy()
21462147
result.index = res_index

pandas/tests/resample/test_resample_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,3 +989,30 @@ def test_resample_empty():
989989
)
990990
result = df.resample("8h").mean()
991991
tm.assert_frame_equal(result, expected)
992+
993+
994+
def test_asfreq_respects_origin_with_fixed_freq_all_seconds_equal():
995+
# GH#62725: Ensure Resampler.asfreq respects origin="start_day"
996+
# when all datetimes share identical seconds values.
997+
idx = [
998+
datetime(2025, 10, 17, 17, 15, 10),
999+
datetime(2025, 10, 17, 17, 16, 10),
1000+
datetime(2025, 10, 17, 17, 17, 10),
1001+
]
1002+
df = DataFrame({"value": [0, 1, 2]}, index=idx)
1003+
1004+
result = df.resample("1min", origin="start_day").asfreq()
1005+
1006+
# Expected index: list of Timestamps, matching dtype
1007+
exp_idx = pd.DatetimeIndex(
1008+
[
1009+
pd.Timestamp("2025-10-17 17:15:00"),
1010+
pd.Timestamp("2025-10-17 17:16:00"),
1011+
pd.Timestamp("2025-10-17 17:17:00"),
1012+
],
1013+
dtype=result.index.dtype,
1014+
freq="min",
1015+
)
1016+
1017+
exp = DataFrame({"value": [np.nan, np.nan, np.nan]}, index=exp_idx)
1018+
tm.assert_frame_equal(result, exp)

0 commit comments

Comments
 (0)