A pandas 2.x pipeline calls `resample('M')` and `resample('H')`; what happens when it runs on pandas 3.0, and how do you fix it?
answer
- deprecated in 2.2, removed in 3.0
- E marks the period end
- hours and minutes are lowercase
- period aliases kept M
basics
~20 sIn pandas 3.0 both raise ValueError: Invalid frequency. The aliases were removed: use 'ME' for month end (also QE, YE, BME, BQE, BYE, SME, CBME) and lowercase 'h' for hours (also min, ms, us, ns for T, L, U, N).
solid answer
~40 sIn pandas 3.0 the offset aliases deprecated in 2.2 are gone, so `resample('M')` raises `ValueError: Invalid frequency: M` with a hint that `'M'` is no longer supported for offsets and `'ME'` should be used, and `resample('H')` raises the same error with "Did you mean h?". The renames are `M`, `Q`, `Y`, `BM`, `BQ`, `BY`, `SM`, `CBM` to `ME`, `QE`, `YE`, `BME`, `BQE`, `BYE`, `SME`, `CBME`, and `H`, `BH`, `CBH`, `T`, `L`, `U`, `N` to `h`, `bh`, `cbh`, `min`, `ms`, `us`, `ns`. They apply wherever a frequency string goes: `resample`, `date_range`, `asfreq`, `pd.Grouper(freq=)`, rolling offsets. Period frequencies keep `'M'`, `'Q'` and `'Y'`: `to_period('M')` is still correct, and `to_period('ME')` raises.
code
python · 14 linesimport pandas as pd
hourly = pd.Series(
1, index=pd.date_range("2026-01-01", periods=24 * 90, freq="h")
)
try:
hourly.resample("M").sum()
except ValueError as err:
print(err) # Invalid frequency: M ... Please use 'ME' instead.
monthly = hourly.resample("ME").sum() # offsets: ME, QE, YE, h, min
periods = monthly.index.to_period("M") # periods: still M, Q, Y
print(monthly.index[0], periods[0]) # 2026-01-31 00:00:00 2026-01go deeper
Remember that in pandas 3.0 month end is 'ME', hours are 'h' and minutes are 'min'; the old 'M', 'H' and 'T' raise an error.
Explain the two families of renames, end-anchored offsets gaining an E and sub-daily aliases going lowercase, and that the error appears wherever a frequency string is parsed.
Plan the migration: surface every alias on 2.2 with warnings as errors, fix only offset frequencies, keep period aliases, and check frequencies stored outside the code.
Weigh a pinned 2.x environment against a migration sprint, and require frequency strings to live in code that the test suite exercises so the next removal fails in CI.
## What changed and when pandas uses short strings called **frequency aliases** for offsets: `'D'` for a calendar day, `'h'` for an hour, `'ME'` for a month end. Two families of old aliases were deprecated in pandas 2.2, which warned but still worked, and **removed in pandas 3.0**: | removed in 3.0 | use instead | offset | |---|---|---| | `M` | `ME` | `MonthEnd` | | `Q` | `QE` | `QuarterEnd` | | `Y` | `YE` | `YearEnd` | | `BM`, `BQ`, `BY` | `BME`, `BQE`, `BYE` | business month, quarter, year end | | `SM`, `CBM` | `SME`, `CBME` | semi-month end, custom business month end | | `H`, `BH`, `CBH` | `h`, `bh`, `cbh` | hour, business hour, custom business hour | | `T` | `min` | minute | | `L`, `U`, `N` | `ms`, `us`, `ns` | milli-, micro-, nanosecond | The `E` suffix makes the **end** anchor explicit, next to the start-anchored `MS`, `QS` and `YS`, which did not change. The sub-daily aliases moved to lowercase so they read like units and match the `Timedelta` unit strings. ## What the failure looks like The 2.x code runs on pandas 3.0 until the first frequency string is parsed, then raises: - `resample('M')` gives `ValueError: Invalid frequency: M. Failed to parse with error message: ValueError("'M' is no longer supported for offsets. Please use 'ME' instead.")`. - `resample('H')` gives `ValueError: Invalid frequency: H. Failed to parse ...` followed by `Did you mean h?`. Because the error is raised when the alias is parsed, it surfaces in any call that takes a frequency, not only in `resample`: - `date_range(..., freq='H')` and `timedelta_range(..., freq='H')`; - `asfreq('M')`, `shift(freq='H')`; - `pd.Grouper(key='ts', freq='M')` inside a `groupby`; - offset windows such as `rolling('2H')`, which raise `ValueError: passed window 2H is not compatible with a datetimelike index`. ## The period exception **Period** frequencies did not change. A `Period` or `PeriodIndex` is a span ("March 2026"), not a timestamp, and its aliases are still `'M'`, `'Q'`, `'Y'`, `'D'`, `'h'`: - `s.index.to_period('M')`, `pd.period_range('2026-01', periods=3, freq='M')` and `dt.to_period('M')` are correct in pandas 3.0. - `to_period('ME')` raises, telling you to use `'M'` for a period. So a blind search-and-replace of `'M'` with `'ME'` across a codebase breaks every `to_period` call it touches. Replace only where the string is an offset frequency. ## Migrating a pipeline 1. **Run the test suite on pandas 2.2 with warnings as errors** (for example `pytest -W error`). Every removed alias still works there but warns, so each one fails a test with its replacement named in the message. 2. **Search for frequency strings** in `resample`, `date_range`, `asfreq`, `Grouper`, `shift(freq=)` and offset windows, and replace them with the new aliases; leave `to_period`, `period_range` and `PeriodIndex` frequencies on the period aliases. 3. **Check configuration and stored strings.** A frequency read from YAML, a database column or a job argument is not found by a code search, and fails only at run time. 4. **Upgrade to 3.0** and run the suite again. ## Other 3.0 frequency changes to know about - Lowercase `'d'`, `'b'` and `'c'` for day, business day and custom business day are **deprecated** in 3.0 in favour of `'D'`, `'B'` and `'C'`; they still work but warn. - `pd.offsets.Day` is now a **calendar day**, so on timezone-aware data a day across a DST change keeps the time of day instead of adding exactly 24 hours. That belongs with the datetime-and-zones material, but it can change `resample('D')` boundaries on aware timestamps. ## Strings pandas writes for you The aliases pandas itself produces already use the new spellings. `pd.infer_freq` returns `'h'` for an hourly index and `'ME'` for month ends, and an index built with `freq='h'` reports `'h'` as its `freqstr`. A pipeline that infers a frequency, stores it and passes it back to pandas therefore keeps working after the upgrade. What breaks is a string a person typed: a literal in code, a job argument, a notebook, a dashboard query template. That is where to look first. ## How to talk about it in an interview Name the release (deprecated in 2.2, removed in 3.0), give the two families of renames, and add the period exception. That last point is what shows you have actually done the migration rather than read the headline.
- A teammate replaced every `'M'` with `'ME'` across the codebase and now `to_period` calls fail. Why?Period frequencies kept their aliases: a `PeriodIndex` spans whole months and uses `'M'`, `'Q'` and `'Y'`. In pandas 3.0 `to_period('ME')` raises a `ValueError` telling you to use `'M'` for a period. Only offset frequencies (`resample`, `date_range`, `asfreq`, `Grouper`) moved to `'ME'`; revert the period calls.
- How do you find every removed alias before the upgrade rather than in production?Run the suite on pandas 2.2 with warnings turned into errors, for example `pytest -W error`. In 2.2 each removed alias still works but warns with its replacement, so each one fails a test. Then search configuration and stored job arguments too, since a frequency read at run time escapes a code search.
- In pandas 3.0 'M' still works with a deprecation warning
- 'M' was renamed to 'ME' everywhere, including periods
- Only resample is affected by the alias removal
- 'H' and 'h' are interchangeable in pandas 3.0
- 'MS' was also renamed in pandas 3.0
pandas 2.2 accepted M, Q, Y, H, T, L, U, N with a deprecation warning; pandas 3.0 removed them for offsets and raises ValueError.