Skip to content

Commit bffc7ad

Browse files
authored
BUG: Make DTI/TDI/PI argsort match their underlying arrays (#37965)
1 parent d08f12c commit bffc7ad

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

pandas/core/groupby/grouper.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
373373
# possibly sort
374374
if (self.sort or sort) and not ax.is_monotonic:
375375
# use stable sort to support first, last, nth
376-
indexer = self.indexer = ax.argsort(kind="mergesort")
376+
# TODO: why does putting na_position="first" fix datetimelike cases?
377+
indexer = self.indexer = ax.array.argsort(
378+
kind="mergesort", na_position="first"
379+
)
377380
ax = ax.take(indexer)
378381
obj = obj.take(indexer, axis=self.axis)
379382

pandas/core/indexes/base.py

-4
Original file line numberDiff line numberDiff line change
@@ -4767,10 +4767,6 @@ def argsort(self, *args, **kwargs) -> np.ndarray:
47674767
>>> idx[order]
47684768
Index(['a', 'b', 'c', 'd'], dtype='object')
47694769
"""
4770-
if needs_i8_conversion(self.dtype):
4771-
# TODO: these do not match the underlying EA argsort methods GH#37863
4772-
return self.asi8.argsort(*args, **kwargs)
4773-
47744770
# This works for either ndarray or EA, is overriden
47754771
# by RangeIndex, MultIIndex
47764772
return self._data.argsort(*args, **kwargs)

pandas/tests/indexes/datetimelike.py

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111

1212
class DatetimeLike(Base):
13+
def test_argsort_matches_array(self):
14+
rng = self.create_index()
15+
rng = rng.insert(1, pd.NaT)
16+
17+
result = rng.argsort()
18+
expected = rng._data.argsort()
19+
tm.assert_numpy_array_equal(result, expected)
20+
1321
def test_can_hold_identifiers(self):
1422
idx = self.create_index()
1523
key = idx[0]

0 commit comments

Comments
 (0)