Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,10 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
# possibly sort
if (self.sort or sort) and not ax.is_monotonic:
# use stable sort to support first, last, nth
indexer = self.indexer = ax.argsort(kind="mergesort")
# TODO: why does putting na_position="first" fix datetimelike cases?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really familiar with the history of NaTs with respect to resample. I would have assumed NaT would/should have been dropped by this point since we just exclude them from the result #13164.

I'm okay with this solution as a stop gap solution.

indexer = self.indexer = ax.array.argsort(
kind="mergesort", na_position="first"
)
ax = ax.take(indexer)
obj = obj.take(indexer, axis=self.axis)

Expand Down
4 changes: 0 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4776,10 +4776,6 @@ def argsort(self, *args, **kwargs) -> np.ndarray:
>>> idx[order]
Index(['a', 'b', 'c', 'd'], dtype='object')
"""
if needs_i8_conversion(self.dtype):
# TODO: these do not match the underlying EA argsort methods GH#37863
return self.asi8.argsort(*args, **kwargs)

# This works for either ndarray or EA, is overriden
# by RangeIndex, MultIIndex
return self._data.argsort(*args, **kwargs)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@


class DatetimeLike(Base):
def test_argsort_matches_array(self):
rng = self.create_index()
rng = rng.insert(1, pd.NaT)

result = rng.argsort()
expected = rng._data.argsort()
tm.assert_numpy_array_equal(result, expected)

def test_can_hold_identifiers(self):
idx = self.create_index()
key = idx[0]
Expand Down