Skip to content

Commit 03284f3

Browse files
committed
API: add in IndexSlice indexer shortcut
1 parent 1068a44 commit 03284f3

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

doc/source/indexing.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,13 @@ axes at the same time.
17851785
dfmi.loc[(slice(None),slice(None), ['C1','C3']),(slice(None),'foo')]
17861786
dfmi.loc[df[('a','foo')]>200,slice(None), ['C1','C3']),(slice(None),'foo')]
17871787
1788+
You can use a ``pd.IndexSlice`` to shortcut the creation of these slices
1789+
1790+
.. ipython:: python
1791+
1792+
idx = pd.IndexSlice
1793+
dfmi.loc[idx[:,:,['C1','C3']],idx[:,'foo']]
1794+
17881795
Furthermore you can *set* the values using these methods
17891796

17901797
.. ipython:: python

doc/source/v0.14.0.txt

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ axes at the same time.
9999
df.loc[(slice(None),slice(None), ['C1','C3']),(slice(None),'foo')]
100100
df.loc[df[('a','foo')]>200,slice(None), ['C1','C3']),(slice(None),'foo')]
101101

102+
You can use a ``pd.IndexSlice`` to shortcut the creation of these slices
103+
104+
.. ipython:: python
105+
106+
idx = pd.IndexSlice
107+
df.loc[idx[:,:,['C1','C3']],idx[:,'foo']]
108+
102109
Furthermore you can *set* the values using these methods
103110

104111
.. ipython:: python

pandas/core/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
WidePanel = Panel
2121

22+
from pandas.core.indexing import IndexSlice
2223
from pandas.tseries.offsets import DateOffset
2324
from pandas.tseries.tools import to_datetime
2425
from pandas.tseries.index import (DatetimeIndex, Timestamp,

pandas/core/indexing.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import numpy as np
1414

15-
1615
# the supported indexers
1716
def get_indexers_list():
1817

@@ -27,6 +26,11 @@ def get_indexers_list():
2726
# "null slice"
2827
_NS = slice(None, None)
2928

29+
# the public IndexSlicerMaker
30+
class _IndexSlice(object):
31+
def __getitem__(self, arg):
32+
return arg
33+
IndexSlice = _IndexSlice()
3034

3135
class IndexingError(Exception):
3236
pass

pandas/tests/test_indexing.py

+16
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,9 @@ def f():
11521152

11531153
def test_per_axis_per_level_getitem_doc_examples(self):
11541154

1155+
# test index maker
1156+
idx = pd.IndexSlice
1157+
11551158
# from indexing.rst / advanced
11561159
def mklbl(prefix,n):
11571160
return ["%s%s" % (prefix,i) for i in range(n)]
@@ -1170,11 +1173,15 @@ def mklbl(prefix,n):
11701173
expected = df.loc[[ tuple([a,b,c,d]) for a,b,c,d in df.index.values if (
11711174
a == 'A1' or a == 'A2' or a == 'A3') and (c == 'C1' or c == 'C3')]]
11721175
assert_frame_equal(result, expected)
1176+
result = df.loc[idx['A1':'A3',:,['C1','C3']],:]
1177+
assert_frame_equal(result, expected)
11731178

11741179
result = df.loc[(slice(None),slice(None), ['C1','C3']),:]
11751180
expected = df.loc[[ tuple([a,b,c,d]) for a,b,c,d in df.index.values if (
11761181
c == 'C1' or c == 'C3')]]
11771182
assert_frame_equal(result, expected)
1183+
result = df.loc[idx[:,:,['C1','C3']],:]
1184+
assert_frame_equal(result, expected)
11781185

11791186
# not sorted
11801187
def f():
@@ -1187,6 +1194,9 @@ def f():
11871194

11881195
def test_per_axis_per_level_setitem(self):
11891196

1197+
# test index maker
1198+
idx = pd.IndexSlice
1199+
11901200
# test multi-index slicing with per axis and per index controls
11911201
index = MultiIndex.from_tuples([('A',1),('A',2),('A',3),('B',1)],
11921202
names=['one','two'])
@@ -1242,6 +1252,12 @@ def test_per_axis_per_level_setitem(self):
12421252
expected.iloc[[0,3],[1,3]] = 100
12431253
assert_frame_equal(df, expected)
12441254

1255+
df = df_orig.copy()
1256+
df.loc[idx[:,1],idx[:,['foo']]] = 100
1257+
expected = df_orig.copy()
1258+
expected.iloc[[0,3],[1,3]] = 100
1259+
assert_frame_equal(df, expected)
1260+
12451261
df = df_orig.copy()
12461262
df.loc['A','a'] = 100
12471263
expected = df_orig.copy()

0 commit comments

Comments
 (0)