Skip to content

Commit 8ea00fb

Browse files
authored
DOC: Example for natural sort using key argument (pandas-dev#36356)
1 parent 078f88e commit 8ea00fb

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

environment.yml

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ dependencies:
106106
- cftime # Needed for downstream xarray.CFTimeIndex test
107107
- pyreadstat # pandas.read_spss
108108
- tabulate>=0.8.3 # DataFrame.to_markdown
109+
- natsort # DataFrame.sort_values
109110
- pip:
110111
- git+https://github.com/pandas-dev/pydata-sphinx-theme.git@master
111112
- git+https://github.com/numpy/numpydoc

pandas/core/generic.py

+26
Original file line numberDiff line numberDiff line change
@@ -4409,6 +4409,32 @@ def sort_values(
44094409
3 NaN 8 4 D
44104410
4 D 7 2 e
44114411
5 C 4 3 F
4412+
4413+
Natural sort with the key argument,
4414+
using the `natsort <https://github.com/SethMMorton/natsort>` package.
4415+
4416+
>>> df = pd.DataFrame({
4417+
... "time": ['0hr', '128hr', '72hr', '48hr', '96hr'],
4418+
... "value": [10, 20, 30, 40, 50]
4419+
... })
4420+
>>> df
4421+
time value
4422+
0 0hr 10
4423+
1 128hr 20
4424+
2 72hr 30
4425+
3 48hr 40
4426+
4 96hr 50
4427+
>>> from natsort import index_natsorted
4428+
>>> df.sort_values(
4429+
... by="time",
4430+
... key=lambda x: np.argsort(index_natsorted(df["time"]))
4431+
... )
4432+
time value
4433+
0 0hr 10
4434+
3 48hr 40
4435+
2 72hr 30
4436+
4 96hr 50
4437+
1 128hr 20
44124438
"""
44134439
raise AbstractMethodError(self)
44144440

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ xarray
7373
cftime
7474
pyreadstat
7575
tabulate>=0.8.3
76+
natsort
7677
git+https://github.com/pandas-dev/pydata-sphinx-theme.git@master
7778
git+https://github.com/numpy/numpydoc
7879
pyflakes>=2.2.0

0 commit comments

Comments
 (0)