You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: Selector union (|) returns columns in a different order on non-Polars backends #4029
When you combine two selectors with |, Polars returns the columns in schema order. The other backends return the left selector's columns first, then the right selector's.
importduckdbimportpandasaspdimportpolarsasplimportpyarrowaspaimportnarwhalsasnwimportnarwhals.selectorsasncsdata= {"a": [1], "b": ["x"], "c": [1.0], "d": [True]}
fornativein (pl.DataFrame(data), pd.DataFrame(data), pa.table(data)):
print(nw.from_native(native).select(ncs.boolean() |ncs.numeric()).columns)
rel=duckdb.sql("select 1 as a, 'x' as b, 1.0::double as c, true as d")
print(nw.from_native(rel).select(ncs.boolean() |ncs.numeric()).columns)
Native Polars (pl.selectors.boolean() | pl.selectors.numeric()) gives ['a', 'c', 'd']. Since Narwhals follows the Polars API, the expected result on every backend is ['a', 'c', 'd'].
Cause
CompliantSelector.__or__ in src/narwhals/_compliant/selectors.py returns the left selector's columns, followed by any right-selector columns not already included. It never puts them back into schema order. & and - don't have this problem because they keep the left selector's order, which is already schema order.
Suggested fix
After combining the two sides, filter the frame's schema by membership in the result, so columns come out in schema order. This should land together with #3621, which adds ^ with the same pattern and the same problem. Tests should include a case where the left selector's columns come after the right selector's in the schema, as in the example above. The current tests miss this because all their cases are already in schema order.
Activity