Skip to content

BUG: Keep the grouped revisions in NewsResults.get_impacts with a dict - #11119

Draft
bashtage wants to merge 5 commits into
statsmodels:mainfrom
bashtage:wip-news-groupby-dict-revisions
Draft

bashtage wants to merge 5 commits into
statsmodels:mainfrom
bashtage:wip-news-groupby-dict-revisions

Conversation

@bashtage

@bashtage bashtage commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Stacked on #11081, merge that first (this PR adds one commit). It changes the groupby block of get_impacts that #11081 changes. No conflict in news.py with #11080, #11082 or the NewsResults tolerance PR; test_news.py conflicts with them only where both append tests at the end of the file (as #11081 does with #11080): keep both blocks.

BUG: Give NewsResults.get_impacts the same rows on every pandas version

get_impacts(groupby=...) sums the impacts of the groups of updated
variables for each update date and stacks the update dates back into the
rows with .stack("update date"). On pandas 2.1 to 2.3 this raised two
FutureWarnings (the previous implementation of stack, and Index.insert
with object dtype). The previous implementation drops the rows that are
all NaN, which pandas 3 keeps, so on pandas 3 a group without impacts on
an update date had a row of NaN: for example, with a revision of unemp in
2009Q2 and new data for realgdp in 2009Q3 only, grouping each variable on
its own gave four rows instead of two.

Changes

  • The update dates are stacked with FUTURE_STACK, as elsewhere in the
    module, and the all-NaN rows are dropped explicitly, so the rows are
    those of pandas < 3 on every version.

Results change on pandas 3 for groups without impacts on an update date:
their rows of NaN are gone. On pandas 2.1 to 2.3, a grouped table without
impacts (for example the impacts of news when there were only revisions)
is empty instead of raising "ValueError: Must pass non-zero number of
levels/codes". pandas < 2.1 is unchanged (FUTURE_STACK is empty there, and
the old stack already drops the all-NaN rows).

Not changed

  • get_impacts without a groupby, and every other row and value: checked
    against pandas 2.3.3 with the base for grouped tables of revisions and
    news, with a function, a dict and a level as groupby, with a date and an
    integer index.

Tests

  • With a revision of one variable and new data for the other, for each
    combination of include_revisions and include_updates, the grouped rows
    are the update dates and groups with impacts, with the impacts of the
    single variable of each group. The case with both fails on the base on
    pandas 3, and every case warns on pandas 2.3.
  • test_get_impacts_groupby no longer ignores the FutureWarnings.

BUG: Keep the grouped revisions in NewsResults.get_impacts with a dict

get_impacts(groupby=...) passes groupby to the groupby method of a table
of the impacts whose rows are the updated and revised variables. When
revisions are grouped (revisions before revisions_details_start, the
default), their impacts are in a row whose variable is "all prior
revisions". pandas drops the rows that a dict does not map, and this
label is not a variable of the model, so a dict that maps every variable
silently dropped the impacts of the grouped revisions: in the bivariate
VAR(1) of the tests with both 2009Q2 values revised (and grouped) and new
2009Q3 values, get_impacts(groupby={"realgdp": "output", "unemp":
"labor"}) had only the 2009Q3 rows, and its column sums missed the impacts
of the revisions (a function or the level name kept the row).

Changes

  • A dict that does not map "all prior revisions" keeps the grouped
    revisions as a group of their own, as the identity function does. The
    user's dict is not changed.
  • versionchanged in the docstring of get_impacts.

Results change for valid input: get_impacts with such a dict has a row
for the grouped revisions on the date of the grouped revisions.

Not changed

  • A dict that maps "all prior revisions", groupby given as a function, a
    level name or a list, and tables without grouped revisions. Other keys
    that a dict does not map (variables of the model) are still dropped, as
    pandas does. pandas groupby ignores the default of a defaultdict, so a
    defaultdict behaves as a dict here.

Tests

  • With all revisions grouped, a dict that does not map them gives a row
    "all prior revisions" with the impacts of the revisions (revision_impacts),
    a row for each group of news with the impacts of its variable, column
    sums equal to the total impacts, and leaves the dict unchanged; a dict
    that maps them gives that group. The first case fails on the base
    (pandas 3.0 and 2.3.3).

  • 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 coverage audit of the state space models, writing the fix and its tests, and checking them (tests failing on the base, 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

get_impacts(groupby=...) sums the impacts of the groups of updated
variables for each update date and stacks the update dates back into the
rows with .stack("update date"). On pandas 2.1 to 2.3 this raised two
FutureWarnings (the previous implementation of stack, and Index.insert
with object dtype). The previous implementation drops the rows that are
all NaN, which pandas 3 keeps, so on pandas 3 a group without impacts on
an update date had a row of NaN: for example, with a revision of unemp in
2009Q2 and new data for realgdp in 2009Q3 only, grouping each variable on
its own gave four rows instead of two.

Changes
- The update dates are stacked with FUTURE_STACK, as elsewhere in the
  module, and the all-NaN rows are dropped explicitly, so the rows are
  those of pandas < 3 on every version.

Results change on pandas 3 for groups without impacts on an update date:
their rows of NaN are gone. On pandas 2.1 to 2.3, a grouped table without
impacts (for example the impacts of news when there were only revisions)
is empty instead of raising "ValueError: Must pass non-zero number of
levels/codes". pandas < 2.1 is unchanged (FUTURE_STACK is empty there, and
the old stack already drops the all-NaN rows).

Not changed
- get_impacts without a groupby, and every other row and value: checked
  against pandas 2.3.3 with the base for grouped tables of revisions and
  news, with a function, a dict and a level as groupby, with a date and an
  integer index.

Tests
- With a revision of one variable and new data for the other, for each
  combination of include_revisions and include_updates, the grouped rows
  are the update dates and groups with impacts, with the impacts of the
  single variable of each group. The case with both fails on the base on
  pandas 3, and every case warns on pandas 2.3.
- test_get_impacts_groupby no longer ignores the FutureWarnings.
…ndas < 2

With include_revisions=False and include_updates=False the impacts table
is empty, and with groupby, get_impacts raised "ValueError: Must pass
non-zero number of levels/codes" on pandas 1.5.3 (stack of the empty
table; the Azure minimum-versions job, build 10641,
test_get_impacts_groupby_rows[False-False]). An empty table has nothing to
group, so it is returned as it is: the same empty table, with the index
levels "update date" and "updated variable", that pandas 2 and later give.
get_impacts(groupby=...) passes groupby to the groupby method of a table
of the impacts whose rows are the updated and revised variables. When
revisions are grouped (revisions before revisions_details_start, the
default), their impacts are in a row whose variable is "all prior
revisions". pandas drops the rows that a dict does not map, and this
label is not a variable of the model, so a dict that maps every variable
silently dropped the impacts of the grouped revisions: in the bivariate
VAR(1) of the tests with both 2009Q2 values revised (and grouped) and new
2009Q3 values, get_impacts(groupby={"realgdp": "output", "unemp":
"labor"}) had only the 2009Q3 rows, and its column sums missed the impacts
of the revisions (a function or the level name kept the row).

Changes
- A dict that does not map "all prior revisions" keeps the grouped
  revisions as a group of their own, as the identity function does. The
  user's dict is not changed.
- versionchanged in the docstring of get_impacts.

Results change for valid input: get_impacts with such a dict has a row
for the grouped revisions on the date of the grouped revisions.

Not changed
- A dict that maps "all prior revisions", groupby given as a function, a
  level name or a list, and tables without grouped revisions. Other keys
  that a dict does not map (variables of the model) are still dropped, as
  pandas does. pandas groupby ignores the default of a defaultdict, so a
  defaultdict behaves as a dict here.

Tests
- With all revisions grouped, a dict that does not map them gives a row
  "all prior revisions" with the impacts of the revisions (revision_impacts),
  a row for each group of news with the impacts of its variable, column
  sums equal to the total impacts, and leaves the dict unchanged; a dict
  that maps them gives that group. The first case fails on the base
  (pandas 3.0 and 2.3.3).
@bashtage
bashtage force-pushed the wip-news-groupby-dict-revisions branch from 243c79d to 0fde59d Compare November 28, 2026 21:36
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