ManvithPanyam opened a new issue, #41551:
URL: https://github.com/apache/beam/issues/41551

   ## Observed behavior
   
   Beam's deferred DataFrame and Series `xs()` can return a MultiIndex with the
   levels in a different order from the input when `level` is supplied in a
   non-original order and `drop_level=False`.
   
   The issue affects both partial-key partitioned evaluation and full-key
   evaluation. For example, after selecting levels in the order `['lvl1',
   'lvl0']`, the pre-fix Beam result can expose `['lvl1', 'lvl0']` as its index
   level order even though the input order was `['lvl0', 'lvl1']`.
   
   ## Expected native pandas behavior
   
   Pandas preserves all index levels, their names, and their original order when
   `drop_level=False`:
   
   ```python
   import pandas as pd
   
   idx = pd.MultiIndex.from_tuples(
       [('a', 'x'), ('b', 'y')], names=['lvl0', 'lvl1'])
   df = pd.DataFrame({'value': [1, 2]}, index=idx)
   
   result = df.xs(
       ('x', 'a'), level=['lvl1', 'lvl0'], drop_level=False)
   
   assert result.index.names == ['lvl0', 'lvl1']
   assert result.index.tolist() == [('a', 'x')]
   ```
   
   The same metadata-preservation rule applies to `Series.xs()`.
   
   ## Minimal Beam reproduction
   
   The corresponding Beam DataFrame/Series expressions are:
   
   ```python
   beam_df.xs(
       ('x', 'a'), level=['lvl1', 'lvl0'], drop_level=False)
   beam_series.xs(
       ('x', 'a'), level=['lvl1', 'lvl0'], drop_level=False)
   ```
   
   In a Beam DataFrame test, `beam_df` and `beam_series` use the MultiIndex 
proxy
   shown above. Before the fix, evaluating either expression could produce a
   result whose `index.names` were `['lvl1', 'lvl0']`; native pandas returns
   `['lvl0', 'lvl1']`.
   
   ## Root cause
   
   Beam temporarily moves the requested levels to the front so it can partition
   by those levels. The original implementation did not consistently apply the
   inverse permutation after `pandas.xs()` when `drop_level=False`. The full-key
   path was particularly easy to miss because it wraps matching partition
   results in a singleton stage to handle pandas' data-dependent dimensionality
   reduction.
   
   ## Affected paths
   
   - `DeferredDataFrame.xs(..., level=..., drop_level=False)`
   - `DeferredSeries.xs(..., level=..., drop_level=False)`
   - Partial-key and full-key MultiIndex selections
   
   ## Verification environment
   
   - Apache Beam checkout at commit `5d834f2781bc1b73e33178d39d9e71512def2c41`
   - Python 3.14.6 on Windows
   - pandas 2.3.3
   - Targeted xs tests: 5 passed
   - Full `sdks/python/apache_beam/dataframe/frames_test.py`: 462 passed,
     19 skipped
   
   ## Proposed fix
   
   Record the permutation used for temporary level reordering and restore it on
   `drop_level=False` DataFrame/Series results in both the partitioned and 
full-key
   paths. Leave scalar and other dimensionality-reduced results unchanged. Add
   regression coverage for DataFrame and Series, named and unnamed levels,
   duplicate labels, partial keys, full keys with reversed level order, and
   `drop_level=True` compatibility.
   
   This report is independent of Pandas 3.x compatibility Issue #40709 and PR
   #40768.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to