Skip to content

Commit 30ec359

Browse files
committed
DOC: .get_slice_bound in MultiIndex needs documentation. (pandas-dev#29967)
1 parent 18631d6 commit 30ec359

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

pandas/core/indexes/multi.py

+47-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict
22
import datetime
33
from sys import getsizeof
4+
from typing import Hashable, Sequence, Union
45
import warnings
56

67
import numpy as np
@@ -1266,17 +1267,10 @@ def _get_grouper_for_level(self, mapper, level):
12661267
# Remove unobserved levels from level_index
12671268
level_index = level_index.take(uniques)
12681269

1269-
<<<<<<< HEAD
1270-
if not level_index.tolist():
1271-
grouper = level_index
1272-
else:
1273-
grouper = level_index.take(codes)
1274-
=======
12751270
if len(level_index):
12761271
grouper = level_index.take(codes)
12771272
else:
12781273
grouper = level_index.take(codes, fill_value=True)
1279-
>>>>>>> upstream/master
12801274

12811275
return grouper, codes, level_index
12821276

@@ -2436,8 +2430,53 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
24362430

24372431
return target, indexer
24382432

2439-
def get_slice_bound(self, label, side, kind):
2433+
def get_slice_bound(
2434+
self, label: Union[Hashable, Sequence[Hashable]], side: str, kind: str
2435+
) -> int:
2436+
"""
2437+
For an ordered MultiIndex, compute slice bound
2438+
that corresponds to given label.
2439+
2440+
Returns leftmost (one-past-the-rightmost if `side=='right') position
2441+
of given label.
24402442
2443+
Parameters
2444+
----------
2445+
label : object or tuple of objects
2446+
side : {'left', 'right'}
2447+
kind : {'loc', 'getitem'}
2448+
2449+
Returns
2450+
-------
2451+
int
2452+
Index of label.
2453+
2454+
Notes
2455+
-----
2456+
This method only works if level 0 index of the MultiIndex is lexsorted.
2457+
2458+
Examples
2459+
--------
2460+
>>> mi = pd.MultiIndex.from_arrays([list('abbc'), list('gefd')])
2461+
2462+
Get the locations from the leftmost 'b' in the first level
2463+
until the end of the multiindex:
2464+
2465+
>>> mi.get_slice_bound('b', side="left", kind="loc")
2466+
1
2467+
2468+
Like above, but if you get the locations from the rightmost
2469+
'b' in the first level and 'f' in the second level:
2470+
2471+
>>> mi.get_slice_bound(('b','f'), side="right", kind="loc")
2472+
3
2473+
2474+
See Also
2475+
--------
2476+
MultiIndex.get_loc : Get location for a label or a tuple of labels.
2477+
MultiIndex.get_locs : Get location for a label/slice/list/mask or a
2478+
sequence of such.
2479+
"""
24412480
if not isinstance(label, tuple):
24422481
label = (label,)
24432482
return self._partial_tup_index(label, side=side)

pandas/tests/indexes/test_base.py

-24
Original file line numberDiff line numberDiff line change
@@ -2233,30 +2233,6 @@ def test_groupby(self):
22332233

22342234
tm.assert_dict_equal(result, expected)
22352235

2236-
def test_groupby_nan_index_value(self):
2237-
df = pd.DataFrame([["x", np.nan, 1]], columns=["A", "B", "C"]).set_index(
2238-
["A", "B"]
2239-
)
2240-
result = df.groupby(level=["A", "B"]).C.sum()
2241-
result = np.asarray(result)
2242-
2243-
s = Series([])
2244-
s.name = "C"
2245-
expected = s.astype("int64")
2246-
expected = np.asarray(expected)
2247-
tm.assert_numpy_array_equal(result, expected)
2248-
2249-
df = pd.DataFrame(
2250-
[["x", np.nan, 1, 2], [None, "y", 3, 4]], columns=["A", "B", "C", "D"]
2251-
).set_index(["A", "B", "C"])
2252-
result = df.groupby(level=["A", "B"]).D.sum()
2253-
s = Series([])
2254-
s.name = "D"
2255-
expected = s.astype("int64")
2256-
result = np.asarray(result)
2257-
expected = np.asarray(expected)
2258-
tm.assert_numpy_array_equal(result, expected)
2259-
22602236
@pytest.mark.parametrize(
22612237
"mi,expected",
22622238
[

0 commit comments

Comments
 (0)