Skip to content

Commit 12dafcf

Browse files
committed
CLN: move sort_index to core/generic.py pandas-dev#8283
1 parent c688a0f commit 12dafcf

File tree

3 files changed

+115
-109
lines changed

3 files changed

+115
-109
lines changed

pandas/core/frame.py

+11-56
Original file line numberDiff line numberDiff line change
@@ -5466,62 +5466,17 @@ def sort_index(
54665466
C 3
54675467
d 4
54685468
"""
5469-
# TODO: this can be combined with Series.sort_index impl as
5470-
# almost identical
5471-
5472-
inplace = validate_bool_kwarg(inplace, "inplace")
5473-
5474-
axis = self._get_axis_number(axis)
5475-
labels = self._get_axis(axis)
5476-
labels = ensure_key_mapped(labels, key, levels=level)
5477-
5478-
# make sure that the axis is lexsorted to start
5479-
# if not we need to reconstruct to get the correct indexer
5480-
labels = labels._sort_levels_monotonic()
5481-
if level is not None:
5482-
new_axis, indexer = labels.sortlevel(
5483-
level, ascending=ascending, sort_remaining=sort_remaining
5484-
)
5485-
5486-
elif isinstance(labels, MultiIndex):
5487-
from pandas.core.sorting import lexsort_indexer
5488-
5489-
indexer = lexsort_indexer(
5490-
labels._get_codes_for_sorting(),
5491-
orders=ascending,
5492-
na_position=na_position,
5493-
)
5494-
else:
5495-
from pandas.core.sorting import nargsort
5496-
5497-
# Check monotonic-ness before sort an index
5498-
# GH11080
5499-
if (ascending and labels.is_monotonic_increasing) or (
5500-
not ascending and labels.is_monotonic_decreasing
5501-
):
5502-
if inplace:
5503-
return
5504-
else:
5505-
return self.copy()
5506-
5507-
indexer = nargsort(
5508-
labels, kind=kind, ascending=ascending, na_position=na_position
5509-
)
5510-
5511-
baxis = self._get_block_manager_axis(axis)
5512-
new_data = self._mgr.take(indexer, axis=baxis, verify=False)
5513-
5514-
# reconstruct axis if needed
5515-
new_data.axes[baxis] = new_data.axes[baxis]._sort_levels_monotonic()
5516-
5517-
if ignore_index:
5518-
new_data.axes[1] = ibase.default_index(len(indexer))
5519-
5520-
result = self._constructor(new_data)
5521-
if inplace:
5522-
return self._update_inplace(result)
5523-
else:
5524-
return result.__finalize__(self, method="sort_index")
5469+
return super().sort_index(
5470+
axis,
5471+
level,
5472+
ascending,
5473+
inplace,
5474+
kind,
5475+
na_position,
5476+
sort_remaining,
5477+
ignore_index,
5478+
key,
5479+
)
55255480

55265481
def value_counts(
55275482
self,

pandas/core/generic.py

+93
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
CompressionOptions,
4141
FilePathOrBuffer,
4242
FrameOrSeries,
43+
IndexKeyFunc,
4344
JSONSerializable,
4445
Label,
4546
Level,
@@ -68,6 +69,7 @@
6869
from pandas.core.dtypes.common import (
6970
ensure_int64,
7071
ensure_object,
72+
ensure_platform_int,
7173
ensure_str,
7274
is_bool,
7375
is_bool_dtype,
@@ -96,6 +98,7 @@
9698
import pandas.core.common as com
9799
from pandas.core.construction import create_series_with_explicit_dtype
98100
from pandas.core.flags import Flags
101+
from pandas.core.indexes import base as ibase
99102
from pandas.core.indexes.api import Index, MultiIndex, RangeIndex, ensure_index
100103
from pandas.core.indexes.datetimes import DatetimeIndex
101104
from pandas.core.indexes.period import Period, PeriodIndex
@@ -104,6 +107,7 @@
104107
from pandas.core.missing import find_valid_index
105108
from pandas.core.ops import _align_method_FRAME
106109
from pandas.core.shared_docs import _shared_docs
110+
from pandas.core.sorting import ensure_key_mapped
107111
from pandas.core.window import Expanding, ExponentialMovingWindow, Rolling, Window
108112

109113
from pandas.io.formats import format as fmt
@@ -4418,6 +4422,95 @@ def sort_values(
44184422
"""
44194423
raise AbstractMethodError(self)
44204424

4425+
def sort_index(
4426+
self,
4427+
axis=0,
4428+
level=None,
4429+
ascending: bool_t = True,
4430+
inplace: bool_t = False,
4431+
kind: str = "quicksort",
4432+
na_position: str = "last",
4433+
sort_remaining: bool_t = True,
4434+
ignore_index: bool_t = False,
4435+
key: IndexKeyFunc = None,
4436+
):
4437+
4438+
inplace = validate_bool_kwarg(inplace, "inplace")
4439+
4440+
is_dataframe = isinstance(self, ABCDataFrame)
4441+
if is_dataframe:
4442+
axis = self._get_axis_number(axis)
4443+
labels = self._get_axis(axis)
4444+
labels = ensure_key_mapped(labels, key, levels=level)
4445+
labels = labels._sort_levels_monotonic()
4446+
target = labels
4447+
else:
4448+
_ = self._get_axis_number(axis)
4449+
index = ensure_key_mapped(self.index, key, levels=level)
4450+
target = index
4451+
4452+
if level is not None:
4453+
new_index, indexer = target.sortlevel(
4454+
level, ascending=ascending, sort_remaining=sort_remaining
4455+
)
4456+
4457+
elif isinstance(target, MultiIndex):
4458+
from pandas.core.sorting import lexsort_indexer
4459+
4460+
if not is_dataframe:
4461+
target = target._sort_levels_monotonic()
4462+
4463+
indexer = lexsort_indexer(
4464+
target._get_codes_for_sorting(),
4465+
orders=ascending,
4466+
na_position=na_position,
4467+
)
4468+
4469+
else:
4470+
from pandas.core.sorting import nargsort
4471+
4472+
# Check monotonic-ness before sort an index
4473+
# GH11080
4474+
if (ascending and target.is_monotonic_increasing) or (
4475+
not ascending and target.is_monotonic_decreasing
4476+
):
4477+
if inplace:
4478+
return
4479+
else:
4480+
return self.copy()
4481+
4482+
indexer = nargsort(
4483+
target, kind=kind, ascending=ascending, na_position=na_position
4484+
)
4485+
4486+
if is_dataframe:
4487+
baxis = self._get_block_manager_axis(axis)
4488+
new_data = self._mgr.take(indexer, axis=baxis, verify=False)
4489+
4490+
# reconstruct axis if needed
4491+
new_data.axes[baxis] = new_data.axes[baxis]._sort_levels_monotonic()
4492+
4493+
if ignore_index:
4494+
new_data.axes[1] = ibase.default_index(len(indexer))
4495+
4496+
result = self._constructor(new_data)
4497+
4498+
else:
4499+
indexer = ensure_platform_int(indexer)
4500+
new_index = self.index.take(indexer)
4501+
new_index = new_index._sort_levels_monotonic()
4502+
4503+
new_values = self._values.take(indexer)
4504+
result = self._constructor(new_values, index=new_index)
4505+
4506+
if ignore_index:
4507+
result.index = ibase.default_index(len(result))
4508+
4509+
if inplace:
4510+
return self._update_inplace(result)
4511+
else:
4512+
return result.__finalize__(self, method="sort_index")
4513+
44214514
@doc(
44224515
klass=_shared_doc_kwargs["klass"],
44234516
axes=_shared_doc_kwargs["axes"],

pandas/core/series.py

+11-53
Original file line numberDiff line numberDiff line change
@@ -3462,59 +3462,17 @@ def sort_index(
34623462
dtype: int64
34633463
"""
34643464

3465-
# TODO: this can be combined with DataFrame.sort_index impl as
3466-
# almost identical
3467-
inplace = validate_bool_kwarg(inplace, "inplace")
3468-
# Validate the axis parameter
3469-
self._get_axis_number(axis)
3470-
index = ensure_key_mapped(self.index, key, levels=level)
3471-
3472-
if level is not None:
3473-
new_index, indexer = index.sortlevel(
3474-
level, ascending=ascending, sort_remaining=sort_remaining
3475-
)
3476-
3477-
elif isinstance(index, MultiIndex):
3478-
from pandas.core.sorting import lexsort_indexer
3479-
3480-
labels = index._sort_levels_monotonic()
3481-
3482-
indexer = lexsort_indexer(
3483-
labels._get_codes_for_sorting(),
3484-
orders=ascending,
3485-
na_position=na_position,
3486-
)
3487-
else:
3488-
from pandas.core.sorting import nargsort
3489-
3490-
# Check monotonic-ness before sort an index
3491-
# GH11080
3492-
if (ascending and index.is_monotonic_increasing) or (
3493-
not ascending and index.is_monotonic_decreasing
3494-
):
3495-
if inplace:
3496-
return
3497-
else:
3498-
return self.copy()
3499-
3500-
indexer = nargsort(
3501-
index, kind=kind, ascending=ascending, na_position=na_position
3502-
)
3503-
3504-
indexer = ensure_platform_int(indexer)
3505-
new_index = self.index.take(indexer)
3506-
new_index = new_index._sort_levels_monotonic()
3507-
3508-
new_values = self._values.take(indexer)
3509-
result = self._constructor(new_values, index=new_index)
3510-
3511-
if ignore_index:
3512-
result.index = ibase.default_index(len(result))
3513-
3514-
if inplace:
3515-
self._update_inplace(result)
3516-
else:
3517-
return result.__finalize__(self, method="sort_index")
3465+
return super().sort_index(
3466+
axis,
3467+
level,
3468+
ascending,
3469+
inplace,
3470+
kind,
3471+
na_position,
3472+
sort_remaining,
3473+
ignore_index,
3474+
key,
3475+
)
35183476

35193477
def argsort(self, axis=0, kind="quicksort", order=None) -> "Series":
35203478
"""

0 commit comments

Comments
 (0)