fix: support pandas >= 3.0.0 Day offset division for resampled time coordinates (#179)#202
Open
raywcm wants to merge 1 commit into
Open
fix: support pandas >= 3.0.0 Day offset division for resampled time coordinates (#179)#202raywcm wants to merge 1 commit into
raywcm wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
pandas 3.0.0 removed support for dividing
Dayoffsets (pd.tseries.frequencies.to_offset(freq) / 2), which causes_calc_resampled_time_coord()to raiseTypeError: unsupported operand type(s) for /: 'pandas._libs.tslibs.offsets.Day' and 'int'.The existing workaround (PR #180) pinned pandas to <3.0.0, but this PR provides a proper fix that works across pandas versions.
Root Cause
The function computes the midpoint of each resampled time period by dividing the frequency offset in half. For example, daily data is shifted by 12 hours to produce noon timestamps. pandas 3.0.0 no longer allows arithmetic division of
Dayoffset objects.Fix
Instead of dividing the offset directly, compute the half-offset duration by anchoring the offset to a reference timestamp:
This produces a
Timedeltaequal to half the offset period, which works identically when added to aDatetimeIndex. The approach is compatible with all pandas versions (2.x and 3.x) and all fixed-length frequency types (D, H, T, S, etc.).Type of change
How Has This Been Tested?
_calc_resampled_time_coord(1D and 5D cases) still pass with the new implementation. The logic produces identical results because(anchor + offset - anchor) / 2yields the same half-period duration asoffset / 2did.pandas.Timestamp("2023-11-01") + (pd.Timestamp("2000-01-01") + pd.tseries.offsets.Day(1) - pd.Timestamp("2000-01-01")) / 2returnsTimestamp('2023-11-01 12:00:00'), matching the expected behavior.Checklist:
Closes #179