Skip to content

Conversation

@coroa
Copy link
Owner

@coroa coroa commented Feb 26, 2025

Closes #64 .

Support passing a multiindex for filtering to isin.

import pandas as pd
import pandas_indexing as pix

df = pd.DataFrame(
    dict(
        a=[1, 2, 3, 4, 5, 6, 7, 8, 9],
        b=[9, 8, 7, 6, 5, 4, 3, 2, 1],
        c=[4, 2, 5, 4, 6, 5, 7, 6, 8],
        d=[1, 4, 2, 5, 3, 6, 4, 7, 5],
    )
).set_index(["a", "b", "c"])
select_this_sub_levels = pd.MultiIndex.from_tuples([(8, 2), (2, 6)], names=["b", "c"])

df.loc[pix.isin(select_this_sub_levels)]

Can not be mixed with other filters in the same isin call:

pix.isin(select_this_sub_levels, b=3)

will not build a selector, but provide a boolean mask of where select_this_sub_levels has b=3 (nowhere). Required for backward compatibility.

It is still possible to achieve the same effect with

df.loc[pix.isin(select_this_sub_levels) & pix.isin(b=3)]

Todo

  • Update guide
  • Update docstring
  • Release notes

@codecov-commenter
Copy link

codecov-commenter commented Feb 26, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 82.75862% with 5 lines in your changes missing coverage. Please review.

Project coverage is 61.26%. Comparing base (1aff82c) to head (273a7d0).

Files with missing lines Patch % Lines
src/pandas_indexing/selectors.py 88.00% 2 Missing and 1 partial ⚠️
src/pandas_indexing/iamc/resolver.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #68      +/-   ##
==========================================
+ Coverage   60.78%   61.26%   +0.48%     
==========================================
  Files          11       11              
  Lines        1150     1172      +22     
  Branches      185      190       +5     
==========================================
+ Hits          699      718      +19     
- Misses        422      424       +2     
- Partials       29       30       +1     

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

@coroa coroa force-pushed the feat/add-multiindex-selector-to-isin branch from 23c5efd to d219b8f Compare February 28, 2025 00:33
@coroa coroa merged commit 354e9b1 into main Feb 28, 2025
6 checks passed
@coroa coroa deleted the feat/add-multiindex-selector-to-isin branch February 28, 2025 00:46
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.

Add support for indexing with a multi-index with a sub-set of levels

3 participants