Repository navigation
BUG: Select the whole quarter for "YYYYQn" keys on date indexes - #10921
Merged
Merged
Conversation
This was referenced Nov 21, 2026
bashtage
force-pushed
the
wip-tsa-quarter-string-keys
branch
from
November 21, 2026 21:40
8adf950 to
7df05fd
Compare
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.
4 of 5 tasks
bashtage
force-pushed
the
wip-tsa-quarter-string-keys
branch
from
November 22, 2026 14:04
7df05fd to
8f773ec
Compare
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
force-pushed
the
wip-tsa-quarter-string-keys
branch
from
November 22, 2026 14:49
8f773ec to
7aac375
Compare
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
force-pushed
the
wip-tsa-quarter-string-keys
branch
from
November 23, 2026 19:21
7aac375 to
6a8e578
Compare
4 of 5 tasks
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.
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.
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 at2001-04 instead of 2001-06, and did the same for a monthly PeriodIndex.
start="2001Q2"started at 2001-06-30 instead of2001-04-01.
start="2001Q2"started in 2000Q2.Changes
of the quarter with
slice_indexer, which pandas does not warn about. Itraises 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.covers the months or days in it, and is the period itself for quarterly
or fiscal-quarter (e.g. Q-NOV) data.
get_prediction_indexturns a slicestartinto its first locationbefore it computes the default end.
predict(start="2001-04")with noendon monthly MS data raised "TypeError: '>' not supported betweeninstances of 'int' and 'slice'" (also on main, and needed here so that
start="2001Q2"without an end keeps working); this is fixed in its owncommit.
versionchangednotes inget_index_locandget_prediction_index.Not changed
of the quarter, as in 0.14.
dynamickey that covers several dates raises a TypeError on main(e.g.
dynamic="2001-04"on daily data).dynamic="2001Q2"on dailydata 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_rangefor 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")equalspredict(12, 17), and on quarterly datadynamic="2001Q2"equalsdynamic=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.
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