Skip to content

Commit 8d8e7f9

Browse files
CLN: fixed formatting, typing, and isort issues
1 parent ac99f7e commit 8d8e7f9

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

pandas/core/frame.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import itertools
1616
import sys
1717
from textwrap import dedent
18-
from typing import FrozenSet, List, Optional, Set, Type, Union, Callable
18+
from typing import Callable, FrozenSet, List, Optional, Set, Type, Union
1919
import warnings
2020

2121
import numpy as np
@@ -4977,7 +4977,7 @@ def sort_values(
49774977
inplace=False,
49784978
kind="quicksort",
49794979
na_position="last",
4980-
key : Callable = None
4980+
key : Union[Callable, None] = None
49814981
):
49824982
inplace = validate_bool_kwarg(inplace, "inplace")
49834983
axis = self._get_axis_number(axis)
@@ -5029,7 +5029,7 @@ def sort_index(
50295029
na_position="last",
50305030
sort_remaining=True,
50315031
by=None,
5032-
key : Callable = None
5032+
key : Union[Callable, None] = None
50335033
):
50345034

50355035
# TODO: this can be combined with Series.sort_index impl as

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime, timedelta
22
import operator
33
from textwrap import dedent
4-
from typing import Union, Callable
4+
from typing import Callable, Union
55
import warnings
66

77
import numpy as np

pandas/core/series.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from io import StringIO
66
from shutil import get_terminal_size
77
from textwrap import dedent
8-
from typing import Any, Callable
8+
from typing import Any, Callable, Union
99
import warnings
1010

1111
import numpy as np
@@ -3019,7 +3019,7 @@ def sort_values(
30193019
inplace=False,
30203020
kind="quicksort",
30213021
na_position="last",
3022-
key : Callable = None
3022+
key: Union[Callable, None] = None
30233023
):
30243024
"""
30253025
Sort by the values.
@@ -3042,7 +3042,7 @@ def sort_values(
30423042
na_position : {'first' or 'last'}, default 'last'
30433043
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
30443044
the end.
3045-
key : Callable, default None
3045+
key : Union[Callable, None], default None
30463046
If not None, apply the key function to every value before
30473047
sorting. Identical to key argument in built-in sorted function.
30483048
@@ -3220,7 +3220,7 @@ def sort_index(
32203220
kind="quicksort",
32213221
na_position="last",
32223222
sort_remaining=True,
3223-
key : Callable = None
3223+
key : Union[Callable, None] = None
32243224
):
32253225
"""
32263226
Sort Series by index labels.
@@ -3249,7 +3249,7 @@ def sort_index(
32493249
sort_remaining : bool, default True
32503250
If True and sorting by level and index is multilevel, sort by other
32513251
levels too (in order) after sorting by specified level.
3252-
key : Callable, default None
3252+
key : Union[Callable, None], default None
32533253
If not None, apply the key function to every index element before
32543254
sorting. Identical to key argument in built-in sorted function.
32553255

pandas/core/sorting.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" miscellaneous sorting / groupby utilities """
2+
from typing import Callable, Union
3+
24
import numpy as np
3-
from typing import Callable
45

56
from pandas._libs import algos, hashtable, lib
67
from pandas._libs.hashtable import unique_label_indices
@@ -189,7 +190,7 @@ def indexer_from_factorized(labels, shape, compress=True):
189190
return get_group_index_sorter(ids, ngroups)
190191

191192

192-
def lexsort_indexer(keys, orders=None, na_position="last", key : Callable = None):
193+
def lexsort_indexer(keys, orders=None, na_position="last", key : Union[Callable, None] = None):
193194
from pandas.core.arrays import Categorical
194195

195196
labels = []
@@ -201,7 +202,7 @@ def lexsort_indexer(keys, orders=None, na_position="last", key : Callable = None
201202

202203
if key:
203204
key_func = np.vectorize(key)
204-
keys = [key_func(key) for key in keys]
205+
keys = [key_func(key) if key.size != 0 else key for key in keys]
205206

206207
for key, order in zip(keys, orders):
207208

@@ -239,7 +240,7 @@ def lexsort_indexer(keys, orders=None, na_position="last", key : Callable = None
239240
return indexer_from_factorized(labels, shape)
240241

241242

242-
def nargsort(items, kind="quicksort", ascending=True, na_position="last", key : Callable = None):
243+
def nargsort(items, kind="quicksort", ascending=True, na_position="last", key: Union[Callable, None] = None):
243244
"""
244245
This is intended to be a drop-in replacement for np.argsort which
245246
handles NaNs. It adds ascending and na_position parameters.
@@ -258,7 +259,12 @@ def nargsort(items, kind="quicksort", ascending=True, na_position="last", key :
258259
if key is not None:
259260
key_func = np.vectorize(key)
260261
masked = np.ma.MaskedArray(items, mask)
261-
vals = np.asarray(key_func(masked)) # revert from masked
262+
263+
if masked.size == 0:
264+
vals = np.array([]) # vectorize fails on empty object arrays
265+
else:
266+
vals = np.asarray(key_func(masked)) # revert from masked
267+
262268
return nargsort(vals, kind=kind, ascending=ascending, na_position=na_position, key=None)
263269

264270
idx = np.arange(len(items))

pandas/tests/frame/test_sorting.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,13 @@ def test_sort_value_key_nan(self):
649649
expected = df.sort_values(1, key=str.lower, ascending=False)
650650
assert_frame_equal(result, expected)
651651

652+
@pytest.mark.parametrize('key', [None, lambda x : x])
653+
def test_sort_value_key_empty(self, key):
654+
df = DataFrame(np.array([]))
655+
656+
df.sort_values(0, key=key)
657+
df.sort_index(key=key)
658+
652659
def test_sort_index(self):
653660
# GH13496
654661

0 commit comments

Comments
 (0)