Skip to content

Indexers

metaframe.DataFrame enable new indexer, applied either on the DataFrame MetaFrames or on the DataFrame itself.

Label-based: mfloc

Indexes MetaFrameRow/MetaFrameCol via .loc, showing selected entries from the DataFrame obj:

  • obj.mfloc[<key>] -> obj.mfr.loc[:, <key>], fall back to obj.mfc.loc[:, <key>]
  • obj.mfloc[<mfr_key>, <mfc_key>] -> obj.mfr.loc[:, <mfr_key>] + obj.mfc.loc[:, <mfc_key>]
>>> dataframe
strings             f  g     h
group               1  0     1
floats bool  group            
1.1    False 0      1  A     2
2.2    False 0      2  B  None
3.3    True  2      3  C     B
4.4    True  1      4  D  None
>>> dataframe.mfr
     floats   bool  group

0       1.1  False      0
1       2.2  False      0
2       3.3   True      2
3       4.4   True      1
>>> dataframe.mfr.loc[:, 'floats']
0    1.1
1    2.2
2    3.3
3    4.4
Name: floats, dtype: float64
>>> dataframe.mfloc['floats']
strings  f  g     h
group    1  0     1
floats             
1.1      1  A     2
2.2      2  B  None
3.3      3  C     B
4.4      4  D  None
>>> dataframe.mfloc[['floats', 'bool'], 'strings']
strings       f  g     h
floats bool             
1.1    False  1  A     2
2.2    False  2  B  None
3.3    True   3  C     B
4.4    True   4  D  None

As a MetaFrame index is always a RangeIndex, row selection are easy but must be explicit:

>>> dataframe.mfloc[(0, 'floats'),] # or dataframe.mfloc[(0, 'floats'),:]
strings  f  g  h
group    1  0  1
floats          
1.1      1  A  2
>>> dataframe.mfloc[(0, 'floats'), (slice(1), 'strings')] # slice must be explicit within tuple
strings  f  g
floats       
1.1      1  A

Notic that slice(1) is inclusive, which is the expected behavior with loc!

Assignments are also enable:

>>> dataframe
strings             f  g     h
group               1  0     1
floats bool  group            
1.1    False 0      1  A     2
2.2    False 0      2  B  None
3.3    True  2      3  C     B
4.4    True  1      4  D  None
>>> dataframe.mfloc['foo'] = 'bar'
>>> dataframe
strings                 f  g     h
group                   1  0     1
floats bool  group foo            
1.1    False 0     bar  1  A     2
2.2    False 0     bar  2  B  None
3.3    True  2     bar  3  C     B
4.4    True  1     bar  4  D  None
>>> dataframe.mfloc[(1, 'group'),] = 3
>>> dataframe
strings                 f  g     h
group                   1  0     1
floats bool  group foo            
1.1    False 0     bar  1  A     2
2.2    False 3     bar  2  B  None
3.3    True  2     bar  3  C     B
4.4    True  1     bar  4  D  None

Positional-based: mfiloc

Indexes MetaFrameRow/MetaFrameCol via .iloc:

  • obj.mfiloc[<key>] -> obj.mfr.iloc[:, <key>], fall back to obj.mfc.iloc[:, <key>]
  • obj.mfiloc[<mfr_key>, <mfc_key>] -> obj.mfr.iloc[:, <mfr_key>] + obj.mfc.iloc[:, <mfc_key>]
>>> dataframe
strings             f  g     h
group               1  0     1
floats bool  group            
1.1    False 0      1  A     2
2.2    False 0      2  B  None
3.3    True  2      3  C     B
4.4    True  1      4  D  None
>>> dataframe.mfr
     floats   bool  group

0       1.1  False      0
1       2.2  False      0
2       3.3   True      2
3       4.4   True      1
>>> dataframe.mfr.iloc[:, 0]
0    1.1
1    2.2
2    3.3
3    4.4
Name: floats, dtype: float64
>>> dataframe.mfiloc[0]
strings  f  g     h
group    1  0     1
floats             
1.1      1  A     2
2.2      2  B  None
3.3      3  C     B
4.4      4  D  None
>>> dataframe.mfloc[[0, 1], 0]
strings       f  g     h
floats bool             
1.1    False  1  A     2
2.2    False  2  B  None
3.3    True   3  C     B
4.4    True   4  D  None

Semantic Query: q

Indexes MetaFrames or self (if it has no MultiIndex) via .query(), showing selected entries from the DataFrame obj:

>>> dataframe
strings             f  g     h
group               1  0     1
floats bool  group            
1.1    False 0      1  A     2
2.2    False 0      2  B  None
3.3    True  2      3  C     B
4.4    True  1      4  D  None
>>> dataframe.q['3 > floats > group']
strings             f  g     h
group               1  0     1
floats bool  group            
1.1    False 0      1  A     2
2.2    False 0      2  B  None
>>> dataframe.q['bool', 'strings.isin(["g", "h"])']
strings            g     h
group              0     1
floats bool group         
3.3    True 2      C     B
4.4    True 1      D  None

Assignments are also enable:

>>> dataframe.q['3 > floats > group'] = [0, 'X', -1]
>>> dataframe
strings             f  g     h
group               1  0     1
floats bool  group            
1.1    False 0      0  X    -1
2.2    False 0      0  X    -1
3.3    True  2      3  C     B
4.4    True  1      4  D  None
>>> dataframe.q[:, 'strings.isin(["g", "h"])', 'foo'] = 'bar'
>>> dataframe
strings              f   g     h
group                1   0     1
foo                nan bar   bar
floats bool  group              
1.1    False 0       1   A     2
2.2    False 0       2   B  None
3.3    True  2       3   C     B
4.4    True  1       4   D  None

Semantic Get-Strings: gs

Indexes MetaFrames or self (if it has no MultiIndex) via Regex Get-Strings (<col_name>:'<regex>'), showing selected entries from the DataFrame obj:

>>> mfr
  strings  integers  floats   bool  ... bool_with_missing  dates_with_missing  mixed_numeric mixed_types
0       a         1     1.1  False  ...             False          2024-02-01              1           1
1       b         2     2.2  False  ...               NaN                 NaN            2.2           a
2       c         3     3.3   True  ...               NaN          2024-02-03              3        3.14
3       d         4     4.4   True  ...              True                 NaN            4.4         NaN
4       e         5     5.5  False  ...               NaN          2024-02-05            NaN  2024-03-01

[5 rows x 14 columns]
>>> mfr.gs["mixed_types:'.*1$'"]
  strings  integers  floats   bool  ... bool_with_missing  dates_with_missing  mixed_numeric mixed_types
0       a         1     1.1  False  ...             False          2024-02-01              1           1
4       e         5     5.5  False  ...               NaN          2024-02-05            NaN  2024-03-01

[2 rows x 14 columns]

Assignments are also enable.