Skip to content

Upgrade to pandas 3, keeping pandas 2 supported - #2955

Merged
Flix6x merged 23 commits into
mainfrom
feat/pandas-3
Dec 3, 2026
Merged

Flix6x merged 23 commits into
mainfrom
feat/pandas-3

Conversation

@Flix6x

@Flix6x Flix6x commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

FlexMeasures moves to pandas 3, and keeps supporting pandas 2. It requires timely-beliefs 5.0.0, the first release that supports pandas 3 (SeitaBV/timely-beliefs#276).

This includes the fix from #2932 (cherry-picked, so its author keeps the credit), as part of one effort to support pandas 3.

Changes

  • Daily data follows calendar days across DST. Pandas 3 turns a resolution of one day into 24 hours, so daily date ranges and resampling drifted by an hour after a DST change. to_calendar_offset counts whole-day resolutions as calendar days again. It is used in initialize_index, the aggregator, profit and process schedulers, and the storage scheduler's resampling. This is a stepping stone, not a correct conversion: a timedelta cannot tell P1D (nominal) from PT24H (absolute), so both count as calendar days, as they did on pandas 2. New tests fail on pandas 3 without it.
  • Epoch values from nanoseconds. Pandas 3 may store datetimes in microseconds, so converting to epoch values now sets the nanosecond unit first.
  • Epoch milliseconds in JSON records. Pandas 3.1 deprecates to_json(date_format="epoch"). with_epoch_milliseconds writes the same JSON that pandas 2 wrote, including how it truncated sub-millisecond values.
  • Smaller pandas 3 fixes:
    • apply_offset_chain returns a Timestamp when given one.
    • Timedeltas are parsed explicitly before unit conversion.
    • Series are indexed by position with iloc.
    • Float columns stay float.
    • Annotation stacking no longer relies on groupby().apply including the grouping columns.
    • Test fixtures write 1h, not 1H.
  • No Pandas4Warnings from FlexMeasures. The code avoids what pandas 3.1 deprecates for pandas 4: inplace=True in forecasting, positional sum arguments, unsorted concat, Timestamp.utcnow and the d unit. The remaining Pandas4Warnings come from darts, and are silent by default outside __main__, because Pandas4Warning is a DeprecationWarning.

Tests

The full suite passes on both pandas versions, with timely-beliefs 5.0.0 from PyPI, after merging main:

pandas Passed Skipped xfailed Failed
3.1.2 4272 7 5 0
2.3.3 4272 7 5 0

New tests:

  • test_to_calendar_offset, test_to_calendar_offset_steps_by_local_midnights_across_dst, test_initialize_index_steps_by_calendar_days_across_dst and test_aggregator_resamples_to_calendar_days_across_dst
  • test_prepare_annotations_for_chart_stacks_annotations_with_the_same_end
  • test_with_epoch_milliseconds_writes_the_json_that_pandas_2_wrote

Speed

From flexmeasures/data/scripts/benchmark_search_postprocessing.py at 100,000 rows, pandas 2.3.3 against pandas 3.1.2 (single run, medians):

Benchmark pandas 2 pandas 3 Change
keep_latest_version (1 source) 32.8 ms 26.0 ms -20.7%
keep_latest_version (3 sources, distinct groups) 33.1 ms 27.0 ms -18.4%
keep_latest_version (3 sources, 2 versions of one) 49.4 ms 42.7 ms -13.6%
deterministic belief per event (3 sources, 2 versions) 42.8 ms 42.4 ms -0.9%
keep_latest_version (probabilistic, 3 sources) 44.9 ms 38.8 ms -13.6%
compress_belief_records (3 sources, distinct groups) 119.2 ms 118.2 ms -0.8%

Lock

uv.lock moves to pandas 3.1.2 (only pandas changes), so CI and the Docker image run on pandas 3. pyproject.toml still allows pandas 2 (pandas>=2.2.1), which no longer runs in CI but passes the suite locally (see above).

🤖 Generated with Claude Code

https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA

furqan-khan07 and others added 15 commits December 2, 2026 13:44
…longer accepts 1H

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…pling

Pandas 3 reads timedelta(days=1) as a frequency of a fixed 24 hours,
so daily steps would no longer land on local midnights after a DST change, as they did under pandas 2.
This would undo #2907 for the GET sensor data endpoint, for example.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
A pd.Timestamp is also a datetime, so it was returned as a datetime.
With pandas 3's zoneinfo timezones, such a datetime in an hour that a DST change repeats
never equals a time in another timezone (PEP 495).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…licitly

Pandas 3 no longer stores them in nanoseconds by default,
so chart data and compressed belief records got timestamps in seconds instead of milliseconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Under pandas 3, text divided by a Timedelta leaves an object dtype rather than floats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Pandas 3 left the grouping column out of each group, and ignored the chained assignment
that collected the contents (Copy-on-Write), so only the first annotation's content was shown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Pandas 3 reads series[3] as a label rather than a position,
and refuses to set a float into an integer column rather than upcasting it.
In the storage scheduler, the constant penalty for preferring a full storage sooner was read with iloc[0][0].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ooded with Pandas4Warnings

No more inplace reset_index, set_index or rename, positional arguments of sum, implicit sorting in concat,
implicit date formats in to_json, Timestamp.utcnow or the d unit alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…eprecates its epoch date format

DataFrame.to_json(date_format="epoch") warns on every call under pandas 3.1, and pandas 3 floors negative durations
where pandas 2 truncated them toward zero. Converting datetime and timedelta columns to milliseconds ourselves
keeps the JSON for charts and annotations exactly as pandas 2 wrote it, without a warning per request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
pyproject still allows pandas 2 (pandas>=2.2.1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x Flix6x added this to the 1.2.0 milestone Dec 2, 2026
@Flix6x Flix6x added upgrading dependencies Pull requests that update a dependency file python Pull requests that update python code labels Dec 2, 2026
@Flix6x Flix6x self-assigned this Dec 2, 2026
@socket-security

socket-security Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedpandas@​2.3.3 ⏵ 3.1.276 +110010010080 -19
Updatedtimely-beliefs@​4.4.1 ⏵ 5.0.0100100100100100

View full report

Flix6x and others added 4 commits December 2, 2026 18:57
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…s a stepping stone

It returns an offset, not a frequency. Its docstring now says it keeps the pandas 2 behaviour rather than converting correctly: a timedelta cannot tell P1D from PT24H, so both count as calendar days, until FlexMeasures keeps nominal and absolute durations apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
These tests fail on pandas 3 without to_calendar_offset: daily steps would fall at 23:00 after DST ends, and the day DST ends would no longer add up 25 hours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3WRq4uE4AaVmZiHWA
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…o longer supports, and say why timely-beliefs 5.0.0 is required

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review before merging. Checked on pandas 2.3.3 and 3.1.2, at 1291e1a:

  • Calendar days across DST. A daily index across the end of DST (25 October 2026, Amsterdam) keeps to local midnights on both versions. With to_calendar_offset returning the 24-hour offset instead, pandas 3 drifts to 23:00 after the change and adds a sixth day, and the four new DST tests fail. So they test the fix.
  • What the PR leaves alone is fine. The forecasting pipelines and the StorageScheduler work in UTC. The reaction-rules index steps by minutes. get_continuous_series hands its resolution to TimedBelief.search, where timely-beliefs 5.0.0 applies the same calendar-day rule.
  • Chart JSON. On 500 random frames (also before 1970, with missing values and negative horizons), with_epoch_milliseconds writes the same bytes on both versions, and the same as pandas 2's own to_json. That holds for microsecond timestamps, which is what the database keeps.
  • Tests. The new tests and the touched parts pass on both versions: utils, planning, reporting, the forecasting pipeline, annotations, the sensor-data API (incl. DST), the assets API and the API schemas. CI is green apart from DCO.

Right after this comment I merge main into the branch (the only conflict is two new tests at the end of test_aggregator.py; both stay, and the reporting tests rerun on both versions before the push), plus one commit that drops initialize_index's branch for pandas < 1.4 (dead since pandas>=2.2.1) and notes in pyproject.toml why timely-beliefs 5.0.0 is required.

DCO fails on the commit from #2932, which had no sign-off there either. I set the check to pass, as that commit is Furqan's own contribution from #2932.

Not this PR's doing: daily values posted across a DST change still land an hour off, on pandas 2 as well (#2503).

Keeps both tests at the end of reporting/tests/test_aggregator.py: the one from #2804 and the DST test from this PR.

Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x

Flix6x commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Follow-ups of the review:

Flix6x and others added 2 commits December 3, 2026 11:57
…er order its sources come in

The lookup used one_deterministic_belief_per_event, which between sources of different types keeps the one listed first.
That order came from the database, so a complete forecast saved before could hide a partial report recorded since,
and test_a_complete_forecast_supersedes_a_partial_report_recorded_since failed whenever it ran on its own (and once in CI).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…tor types

Since #2804, an AggregatorReporter saves each event under the type of its inputs, so one reporter records as a
forecaster, a reporter or a scheduler. Families of sources were keyed by name, type and model, which split such a
reporter into unrelated sources: its versions were not compared, a complete aggregate did not replace a partial one of
the same belief time (the higher source id decided), and a search naming both preferred whichever came first.

- source_family: name and model, plus the type unless it is a data generator's type; used by
  _select_latest_version_and_belief_per_event, keep_latest_version and the aggregator's superseding lookup.
- Sensor.search_data_sources returns sources in id order, not in whatever order the database returns.
- The superseding lookup goes back to one_deterministic_belief_per_event (reverting aafbbef's workaround),
  which test_a_complete_forecast_supersedes_a_partial_report_recorded_since now passes on its own, too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x
Flix6x merged commit a92891c into main Dec 3, 2026
11 checks passed
@Flix6x
Flix6x deleted the feat/pandas-3 branch December 3, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file python Pull requests that update python code upgrading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants