Skip to content

Commit 31097fc

Browse files
committed
add doc-strings, rename sort_monotonic -> sort_levels_monotonic
1 parent 48249ab commit 31097fc

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3349,7 +3349,7 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
33493349

33503350
# make sure that the axis is lexsorted to start
33513351
# if not we need to reconstruct to get the correct indexer
3352-
labels = labels.sort_monotonic()
3352+
labels = labels.sort_levels_monotonic()
33533353

33543354
indexer = lexsort_indexer(labels.labels, orders=ascending,
33553355
na_position=na_position)

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,7 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
17621762
sort_remaining=sort_remaining)
17631763
elif isinstance(index, MultiIndex):
17641764
from pandas.core.sorting import lexsort_indexer
1765-
labels = index.sort_monotonic()
1765+
labels = index.sort_levels_monotonic()
17661766
indexer = lexsort_indexer(labels.labels, orders=ascending)
17671767
else:
17681768
from pandas.core.sorting import nargsort

pandas/indexes/multi.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -1173,14 +1173,15 @@ def from_product(cls, iterables, sortorder=None, names=None):
11731173
labels = cartesian_product(labels)
11741174
return MultiIndex(levels, labels, sortorder=sortorder, names=names)
11751175

1176-
def sort_monotonic(self):
1176+
def sort_levels_monotonic(self):
11771177
"""
11781178
.. versionadded:: 0.20.0
11791179
11801180
This is an *internal* function.
11811181
11821182
create a new MultiIndex from the current to monotonically sorted
1183-
items IN the levels
1183+
items IN the levels. This does not actually make the entire MultiIndex
1184+
monotonic, JUST the levels.
11841185
11851186
The resulting MultiIndex will have the same outward
11861187
appearance, meaning the same .values and ordering. It will also
@@ -1190,6 +1191,19 @@ def sort_monotonic(self):
11901191
-------
11911192
MultiIndex
11921193
1194+
Examples
1195+
--------
1196+
1197+
>>> i = pd.MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
1198+
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
1199+
>>> i
1200+
MultiIndex(levels=[['a', 'b'], ['bb', 'aa']],
1201+
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
1202+
1203+
>>> i.sort_monotonic()
1204+
MultiIndex(levels=[['a', 'b'], ['aa', 'bb']],
1205+
labels=[[0, 0, 1, 1], [1, 0, 1, 0]])
1206+
11931207
"""
11941208

11951209
if self.is_lexsorted() and self.is_monotonic:
@@ -1237,7 +1251,7 @@ def remove_unused_levels(self):
12371251
12381252
Examples
12391253
--------
1240-
>>> i = MultiIndex.from_product([range(2), list('ab')])
1254+
>>> i = pd.MultiIndex.from_product([range(2), list('ab')])
12411255
MultiIndex(levels=[[0, 1], ['a', 'b']],
12421256
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
12431257

pandas/tests/indexes/test_multi.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,7 @@ def test_reconstruct_sort(self):
24202420
assert mi.is_lexsorted()
24212421
assert mi.is_monotonic
24222422

2423-
recons = mi.sort_monotonic()
2423+
recons = mi.sort_levels_monotonic()
24242424
assert recons.is_lexsorted()
24252425
assert recons.is_monotonic
24262426
assert mi is recons
@@ -2435,7 +2435,7 @@ def test_reconstruct_sort(self):
24352435
assert not mi.is_lexsorted()
24362436
assert not mi.is_monotonic
24372437

2438-
recons = mi.sort_monotonic()
2438+
recons = mi.sort_levels_monotonic()
24392439
assert not recons.is_lexsorted()
24402440
assert not recons.is_monotonic
24412441

@@ -2449,7 +2449,7 @@ def test_reconstruct_sort(self):
24492449
assert not mi.is_lexsorted()
24502450
assert not mi.is_monotonic
24512451

2452-
recons = mi.sort_monotonic()
2452+
recons = mi.sort_levels_monotonic()
24532453
assert not recons.is_lexsorted()
24542454
assert not recons.is_monotonic
24552455

pandas/tests/test_multilevel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2582,7 +2582,7 @@ def test_sort_index_and_reconstruction_doc_example(self):
25822582

25832583
# reconstruct
25842584
result = df.sort_index().copy()
2585-
result.index = result.index.sort_monotonic()
2585+
result.index = result.index.sort_levels_monotonic()
25862586
assert result.index.is_lexsorted()
25872587
assert result.index.is_monotonic
25882588

pandas/tests/tools/test_hashing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_multiindex_objects(self):
9191
mi = MultiIndex(levels=[['b', 'd', 'a'], [1, 2, 3]],
9292
labels=[[0, 1, 0, 2], [2, 0, 0, 1]],
9393
names=['col1', 'col2'])
94-
recons = mi.sort_monotonic()
94+
recons = mi.sort_levels_monotonic()
9595

9696
# these are equal
9797
assert mi.equals(recons)

0 commit comments

Comments
 (0)