Skip to content

BUG: Select the whole quarter for "YYYYQn" keys on date indexes - #10921

Merged
bashtage merged 4 commits into
statsmodels:mainfrom
bashtage:wip-tsa-quarter-string-keys
Nov 23, 2026
Merged

bashtage merged 4 commits into
statsmodels:mainfrom
bashtage:wip-tsa-quarter-string-keys

Conversation

@bashtage

Copy link
Copy Markdown
Member

Since #10065 (statsmodels 0.15.0), which avoided pandas' deprecated parsing
of quarter strings as dates, an in-sample quarter key such as "2001Q2"
was converted to the last day of the quarter, or, for a PeriodIndex, to a
quarterly Period. Predictions with such keys were silently wrong where 0.14
used the whole quarter:

  • AutoReg(monthly).predict(start="2001-01", end="2001Q2") stopped at
    2001-04 instead of 2001-06, and did the same for a monthly PeriodIndex.
  • On daily data, start="2001Q2" started at 2001-06-30 instead of
    2001-04-01.
  • On a quarter-start index, start="2001Q2" started in 2000Q2.

Changes

  • DatetimeIndex: a quarter key selects the dates from the start to the end
    of the quarter with slice_indexer, which pandas does not warn about. It
    raises a KeyError if the quarter has no date in the index. A quarter with
    one date, as in quarterly data, still gives an integer location, so
    dynamic="2001Q2" on quarterly data keeps working.
  • PeriodIndex: the string goes to pandas unchanged, as in 0.14. A quarter
    covers the months or days in it, and is the period itself for quarterly
    or fiscal-quarter (e.g. Q-NOV) data.
  • get_prediction_index turns a slice start into its first location
    before it computes the default end. predict(start="2001-04") with no
    end on monthly MS data raised "TypeError: '>' not supported between
    instances of 'int' and 'slice'" (also on main, and needed here so that
    start="2001Q2" without an end keeps working); this is fixed in its own
    commit.
  • versionchanged notes in get_index_loc and get_prediction_index.

Not changed

  • Quarter keys after the end of the sample still resolve to the first date
    of the quarter, as in 0.14.
  • Quarter-end dates and quarterly periods give the same results as before.
  • A dynamic key that covers several dates raises a TypeError on main
    (e.g. dynamic="2001-04" on daily data). dynamic="2001Q2" on daily
    data now does the same instead of using the last day of the quarter.
    That TypeError is fixed separately.

Tests

  • Labels against pd.date_range / pd.period_range for daily, MS, QS,
    QE, monthly-period and quarterly-period indexes, with and without an
    end; mixed month and quarter keys and quarters partly in the sample; an
    integer location for single-date quarters, including Q-NOV; a KeyError
    for quarters with no date.

  • AutoReg: predict(start="2001-01", end="2001Q2") equals
    predict(12, 17), and on quarterly data dynamic="2001Q2" equals
    dynamic=3.

  • 12 of the 20 new tests fail on main.

  • tests added / passed.

  • code/documentation is well formatted.

  • properly formatted commit message. See
    NumPy's guide.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: finding the bug in a contract audit, writing the fix and its tests, and checking them (tests failing on main, mutation check).
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

@bashtage
bashtage force-pushed the wip-tsa-quarter-string-keys branch from 8adf950 to 7df05fd Compare November 21, 2026 21:40
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 21, 2026
For a model whose dates have no frequency (or other labels that are only
row labels), a key that matches several consecutive row labels was
reduced to the location of the first match. As `end` of prediction, a
partial date string such as "2000-01" therefore stopped at the first
January date, a key between two dates such as "2000-02" ended at the next
date (2000-03-06) instead of the previous one, and an end key before the
first date of the start key raised "Prediction must have `end` after
`start`". Repeated labels such as "b" in ["a", "b", "b", "c"] only
covered the first one.

`get_index_label_loc` now keeps pandas' slice, as `get_index_loc` already
does for a date index with a frequency, so that prediction ends at the
last matching label. As `start`, a key still begins at the first one;
the slice of a start key is reduced before the default end is computed
(the same change as in statsmodels#10921), as `max(slice, int)` raised otherwise.
Results change for such end keys.
@bashtage
bashtage force-pushed the wip-tsa-quarter-string-keys branch from 7df05fd to 8f773ec Compare November 22, 2026 14:04
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 22, 2026
For a model whose dates have no frequency (or other labels that are only
row labels), a key that matches several consecutive row labels was
reduced to the location of the first match. As `end` of prediction, a
partial date string such as "2000-01" therefore stopped at the first
January date, a key between two dates such as "2000-02" ended at the next
date (2000-03-06) instead of the previous one, and an end key before the
first date of the start key raised "Prediction must have `end` after
`start`". Repeated labels such as "b" in ["a", "b", "b", "c"] only
covered the first one.

`get_index_label_loc` now keeps pandas' slice, as `get_index_loc` already
does for a date index with a frequency, so that prediction ends at the
last matching label. As `start`, a key still begins at the first one;
the slice of a start key is reduced before the default end is computed
(the same change as in statsmodels#10921), as `max(slice, int)` raised otherwise.
Results change for such end keys.
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 22, 2026
For a model whose dates have no frequency (or other labels that are only
row labels), a key that matches several consecutive row labels was
reduced to the location of the first match. As `end` of prediction, a
partial date string such as "2000-01" therefore stopped at the first
January date, a key between two dates such as "2000-02" ended at the next
date (2000-03-06) instead of the previous one, and an end key before the
first date of the start key raised "Prediction must have `end` after
`start`". Repeated labels such as "b" in ["a", "b", "b", "c"] only
covered the first one.

`get_index_label_loc` now keeps pandas' slice, as `get_index_loc` already
does for a date index with a frequency, so that prediction ends at the
last matching label. As `start`, a key still begins at the first one;
the slice of a start key is reduced before the default end is computed
(the same change as in statsmodels#10921), as `max(slice, int)` raised otherwise.
Results change for such end keys.
@bashtage
bashtage force-pushed the wip-tsa-quarter-string-keys branch from 8f773ec to 7aac375 Compare November 22, 2026 14:49
@bashtage
bashtage marked this pull request as ready for review November 23, 2026 17:01
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 23, 2026
For a model whose dates have no frequency (or other labels that are only
row labels), a key that matches several consecutive row labels was
reduced to the location of the first match. As `end` of prediction, a
partial date string such as "2000-01" therefore stopped at the first
January date, a key between two dates such as "2000-02" ended at the next
date (2000-03-06) instead of the previous one, and an end key before the
first date of the start key raised "Prediction must have `end` after
`start`". Repeated labels such as "b" in ["a", "b", "b", "c"] only
covered the first one.

`get_index_label_loc` now keeps pandas' slice, as `get_index_loc` already
does for a date index with a frequency, so that prediction ends at the
last matching label. As `start`, a key still begins at the first one;
the slice of a start key is reduced before the default end is computed
(the same change as in statsmodels#10921), as `max(slice, int)` raised otherwise.
Results change for such end keys.
A start key that covers several dates, such as "2001-04" for a daily
index or for a month-start index, matches a slice in pandas.
get_prediction_index compared that slice with the last location when no
end was given, so for example SARIMAX(...).predict(start="2001-04") on
monthly MS data raised "TypeError: '>' not supported between instances
of 'int' and 'slice'".

The slice of the start key is now reduced to its first location before
the default end is computed, as it already was afterwards. Prediction
starts at the first date of the key and ends at the end of the sample.

Only input that raised is affected; results of valid input do not change.
This is needed by the next commit, which makes quarter strings such as
"2001Q2" select the whole quarter on a date index.
Since ffd378e (statsmodels 0.15.0), which avoided pandas' deprecated
parsing of quarter strings such as "2001Q2" as dates, an in-sample
quarter key was converted to the last day of the quarter (a DatetimeIndex)
or to a quarterly Period (a PeriodIndex). This gave silently wrong
predictions where statsmodels 0.14 followed pandas' partial-string
indexing and used the whole quarter:

- AutoReg(monthly).predict(start="2001-01", end="2001Q2") stopped at
  2001-04 instead of 2001-06 (also for a monthly PeriodIndex);
- on daily data, start="2001Q2" started at 2001-06-30 instead of
  2001-04-01;
- on a quarter-start index, start="2001Q2" started at 2000-04-01, a
  quarter of the previous year, from pandas' own resolution of the string
  on that frequency.

For a DatetimeIndex, a quarter key now selects the dates from the start to
the end of the quarter with `slice_indexer`, which pandas does not warn
about, and raises a KeyError if the quarter has no date in the index. A
quarter with a single date, as for quarterly data, gives its location as
before. For a PeriodIndex, the string is passed to pandas unchanged as in
statsmodels 0.14 (pandas does not deprecate it there): a quarter covers
all periods of a finer frequency, and is the period of quarterly data.
Quarter keys after the end of the sample are unchanged.

Results change for in-sample quarter keys on daily, weekly, monthly and
quarter-start date indexes and on PeriodIndexes finer than quarterly;
quarter-end dates and quarterly periods give the same results as before.
The test that a quarter key without dates raises a KeyError included
"2001Q2" on an annual PeriodIndex. A key within one period, such as a
quarter of annual periods or a day of quarterly periods, is that period
out of the sample, and is meant to be in the sample too (a separate
fix), so this case is replaced. A quarter key on fiscal quarters
(Q-NOV) is the fiscal quarter of the same label, as in pandas, and
distinguishes the PeriodIndex handling from selecting the periods that
overlap the calendar quarter.
@bashtage
bashtage force-pushed the wip-tsa-quarter-string-keys branch from 7aac375 to 6a8e578 Compare November 23, 2026 19:21
@bashtage
bashtage merged commit 1b41a93 into statsmodels:main Nov 23, 2026
10 of 11 checks passed
@bashtage
bashtage deleted the wip-tsa-quarter-string-keys branch November 24, 2026 15:54
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 24, 2026
A quarter string such as "2000Q2" as the start or end of a prediction
selects the dates of the quarter with index.slice_indexer and the start
and end times of the quarter (statsmodels#10921). For a tz-aware DatetimeIndex these
times were naive, so the lookup raised "TypeError: Cannot compare
tz-naive and tz-aware datetime-like objects" for every in-sample quarter
key, for example res.predict(start="2000Q2", end="2000Q3") on daily
US/Eastern dates. Other date strings are local times of the index since
(_period_end).

Changes
- Localize the start and end times of the quarter to the time zone of the
  index (a no-op for a tz-naive index), so that the key selects the dates
  of the quarter in local time.
- Docstring: the versionchanged note on quarter strings mentions the time
  zone.

Not changed
- tz-naive indexes, PeriodIndex, and the out-of-sample path.

Tests
- Daily, month-start, quarter-start and hourly dates in UTC, US/Eastern
  and Asia/Tokyo: get_index_loc("2000Q2", index) selects the dates whose
  local year and quarter are 2000 and 2 (a single date is an exact match),
  and the returned index ends at the last of them. All fail on main.
- predict(start="2000Q2", end="2000Q3") on daily US/Eastern dates covers
  2000-04-01 to 2000-09-30. Fails on main.
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.

1 participant