Skip to content

Commit ad745c4

Browse files
fixed small bug with datetimelike, updated docs
1 parent 7c6c2f0 commit ad745c4

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
lines changed

doc/source/user_guide/basics.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ used to sort a pandas object by its index levels.
17861786
.. versionadded:: 1.1.0
17871787

17881788
Sorting by index also supports a ``key`` parameter that takes a callable
1789-
function to apply to the index being sorted. for `MultiIndex` objects,
1789+
function to apply to the index being sorted. For `MultiIndex` objects,
17901790
the key is applied per-level to the levels specified by `level`.
17911791

17921792
.. ipython:: python

doc/source/whatsnew/v1.1.0.rst

+5-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ Sorting with keys
4444
We've added a ``key`` argument to the DataFrame and Series sorting methods, including
4545
:meth:`DataFrame.sort_values`, :meth:`DataFrame.sort_index`, :meth:`Series.sort_values`,
4646
and :meth:`Series.sort_index`. The ``key`` can be any callable function which is applied
47-
to the each column of a DataFrame before sorting is performed (:issue:`27237`). See
48-
:ref:`sort_values with keys <basics.sort_value_key>` and :ref:`sort_index with keys
47+
column-by-column to each column used for sorting, before sorting is performed (:issue:`27237`).
48+
See :ref:`sort_values with keys <basics.sort_value_key>` and :ref:`sort_index with keys
4949
<basics.sort_index_key>` for more information.
5050

5151
.. ipython:: python
@@ -54,11 +54,12 @@ to the each column of a DataFrame before sorting is performed (:issue:`27237`).
5454
s.sort_values()
5555
5656
57-
Note how this is sorted with capital letters first. If we apply the `ser.str.lower()` method, we get
57+
Note how this is sorted with capital letters first. If we apply the :meth:`Series.str.lower`
58+
method, we get
5859

5960
.. ipython:: python
6061
61-
s.sort_values(key=lambda x: x.str.lower)
62+
s.sort_values(key=lambda x: x.str.lower())
6263
6364
6465
When applied to a `DataFrame`, they key is applied per-column to all columns or a subset if

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4217,7 +4217,7 @@ def sort_values(
42174217
42184218
Sorting with a key function
42194219
4220-
>>> df.sort_values(by='col4', key=lambda col : col.str.lower())
4220+
>>> df.sort_values(by='col4', key=lambda col: col.str.lower())
42214221
col1 col2 col3 col4
42224222
0 A 2 0 a
42234223
1 A 1 1 B

pandas/core/indexes/datetimelike.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ def sort_values(self, return_indexer=False, ascending=True, key=None):
185185
# NB: using asi8 instead of _data matters in numpy 1.18
186186
# because the treatment of NaT has been changed to put NaT last
187187
# instead of first.
188-
sorted_values = np.sort(idx.asi8)
188+
_as = np.argsort(idx.asi8)
189+
sorted_values = self.asi8[_as]
189190

190-
freq = idx.freq
191+
freq = self.freq
191192
if freq is not None and not is_period_dtype(self):
192193
if freq.n > 0 and not ascending:
193194
freq = freq * -1
@@ -197,7 +198,7 @@ def sort_values(self, return_indexer=False, ascending=True, key=None):
197198
if not ascending:
198199
sorted_values = sorted_values[::-1]
199200

200-
arr = type(idx._data)._simple_new(
201+
arr = type(self._data)._simple_new(
201202
sorted_values, dtype=self.dtype, freq=freq
202203
)
203204
return type(self)._simple_new(arr, name=self.name)

pandas/core/sorting.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -319,32 +319,32 @@ def nargsort(
319319
return indexer
320320

321321

322-
def ensure_key_mapped_multiindex(index, key, level=None):
322+
def ensure_key_mapped_multiindex(index, key: Callable, level=None):
323+
"""
324+
Returns a new MultiIndex in which key has been applied
325+
to all levels specified in level (or all levels if level
326+
is None). Used for key sorting for MultiIndex.
327+
328+
Parameters
329+
----------
330+
index : MultiIndex
331+
Index to which to apply the key function on the
332+
specified levels.
333+
key : Callable
334+
Function that takes an Index and returns an Index of
335+
the same shape. This key is applied to each level
336+
separately. The name of the level can be used to
337+
distinguish different levels for application.
338+
level : list-like, int or str, default None
339+
Level or list of levels to apply the key function to.
340+
If None, key function is applied to all levels. Other
341+
levels are left unchanged.
342+
343+
Returns
344+
-------
345+
labels : MultiIndex
346+
Resulting MultiIndex with modified levels.
323347
"""
324-
Returns a new MultiIndex in which key has been applied
325-
to all levels specified in level (or all levels if level
326-
is None). Used for key sorting for MultiIndex.
327-
328-
Parameters
329-
----------
330-
index : MultiIndex
331-
Index to which to apply the key function on the
332-
specified levels.
333-
key : Callable
334-
Function that takes an Index and returns an Index of
335-
the same shape. This key is applied to each level
336-
separately. The name of the level can be used to
337-
distinguish different levels for application.
338-
level : list-like, int or str, default None
339-
Level or list of levels to apply the key function to.
340-
If None, key function is applied to all levels. Other
341-
levels are left unchanged.
342-
343-
Returns
344-
-------
345-
labels : MultiIndex
346-
Resulting MultiIndex with modified levels.
347-
"""
348348
from pandas.core.indexes.api import MultiIndex
349349

350350
if level is not None:

0 commit comments

Comments
 (0)