Skip to content

fix: support pandas >= 3.0.0 Day offset division for resampled time coordinates (#179)#202

Open
raywcm wants to merge 1 commit into
UBC-MOAD:mainfrom
raywcm:fix/pandas-3.0-day-offset-division
Open

fix: support pandas >= 3.0.0 Day offset division for resampled time coordinates (#179)#202
raywcm wants to merge 1 commit into
UBC-MOAD:mainfrom
raywcm:fix/pandas-3.0-day-offset-division

Conversation

@raywcm

@raywcm raywcm commented Jun 10, 2026

Copy link
Copy Markdown

Description

pandas 3.0.0 removed support for dividing Day offsets (pd.tseries.frequencies.to_offset(freq) / 2), which causes _calc_resampled_time_coord() to raise TypeError: 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 Day offset objects.

Fix

Instead of dividing the offset directly, compute the half-offset duration by anchoring the offset to a reference timestamp:

anchor = pandas.Timestamp("2000-01-01")
offsets = (anchor + offset - anchor) / 2

This produces a Timedelta equal to half the offset period, which works identically when added to a DatetimeIndex. 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • The existing parametrized tests for _calc_resampled_time_coord (1D and 5D cases) still pass with the new implementation. The logic produces identical results because (anchor + offset - anchor) / 2 yields the same half-period duration as offset / 2 did.
  • Manual verification: pandas.Timestamp("2023-11-01") + (pd.Timestamp("2000-01-01") + pd.tseries.offsets.Day(1) - pd.Timestamp("2000-01-01")) / 2 returns Timestamp('2023-11-01 12:00:00'), matching the expected behavior.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Closes #179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError in core.extract._calc_resampled_time_coord()

1 participant