Skip to content

fix(python): Allow list argument in group_by().map_groups()#26707

Merged
nameexhaustion merged 4 commits intopola-rs:mainfrom
stakeswky:fix/map-groups-list-group-by
Mar 10, 2026
Merged

fix(python): Allow list argument in group_by().map_groups()#26707
nameexhaustion merged 4 commits intopola-rs:mainfrom
stakeswky:fix/map-groups-list-group-by

Conversation

@stakeswky
Copy link
Contributor

Fixes #26672

Summary

df.group_by(['a', 'b']).map_groups(lambda df: df) raises TypeError: cannot call map_groups when grouping by an expression, even though all elements are plain column name strings.

Root Cause

group_by accepts *by (variadic), so df.group_by(['a', 'b']) stores self.by = (['a', 'b'],). The check isinstance(c, str) for c in self.by iterates over the outer tuple and finds a list, not a string.

Fix

Flatten self.by via _parse_inputs_as_iterable (already imported and used elsewhere in the module) before the string check. This is consistent with how other methods handle the variadic *by parameter.

As @ritchie46 noted in the issue, the check was overly strict — a list of strings is a perfectly valid input.

Test

Added test_map_groups_group_by_list_26672 verifying both group_by(['a', 'b']) and group_by('a', 'b') work with map_groups.

When `group_by` receives a list of column names (e.g. `df.group_by(['a', 'b'])`),
`self.by` is stored as `(['a', 'b'],)`. The `map_groups` method checked
`isinstance(c, str) for c in self.by` which found the list element and
raised TypeError.

Fix: flatten `self.by` via `_parse_inputs_as_iterable` (already used
elsewhere) before the string check, consistent with how other GroupBy
methods handle the variadic `*by` parameter.

Fixes pola-rs#26672
@stakeswky stakeswky changed the title fix(python): allow list argument in group_by().map_groups() fix(python): Allow list argument in group_by().map_groups() Feb 26, 2026
@github-actions github-actions bot added fix Bug fix python Related to Python Polars and removed title needs formatting labels Feb 26, 2026
@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.66%. Comparing base (9d60226) to head (65ac835).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/src/polars/dataframe/group_by.py 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #26707      +/-   ##
==========================================
+ Coverage   81.00%   81.66%   +0.66%     
==========================================
  Files        1805     1805              
  Lines      248021   248090      +69     
  Branches     3132     3131       -1     
==========================================
+ Hits       200902   202612    +1710     
+ Misses      46313    44673    -1640     
+ Partials      806      805       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nameexhaustion nameexhaustion merged commit 3ea81c4 into pola-rs:main Mar 10, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix python Related to Python Polars RC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

map_groups errors when group_by is passed a list

2 participants