Skip to content

Commit

Permalink
BUG: ignoring sort in DTA.factorize (pandas-dev#53992)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and im-vinicius committed Jul 8, 2023
1 parent 3f26bf6 commit 1dfcf11
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,15 @@ def factorize(
codes = codes[::-1]
uniques = uniques[::-1]
return codes, uniques
# FIXME: shouldn't get here; we are ignoring sort

if sort:
# algorithms.factorize only passes sort=True here when freq is
# not None, so this should not be reached.
raise NotImplementedError(
f"The 'sort' keyword in {type(self).__name__}.factorize is "
"ignored unless arr.freq is not None. To factorize with sort, "
"call pd.factorize(obj, sort=True) instead."
)
return super().factorize(use_na_sentinel=use_na_sentinel)

@classmethod
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,16 @@ def test_iter_zoneinfo_fold(self, tz):
right2 = dta.astype(object)[2]
assert str(left) == str(right2)
assert left.utcoffset() == right2.utcoffset()


def test_factorize_sort_without_freq():
dta = DatetimeArray._from_sequence([0, 2, 1])

msg = r"call pd.factorize\(obj, sort=True\) instead"
with pytest.raises(NotImplementedError, match=msg):
dta.factorize(sort=True)

# Do TimedeltaArray while we're here
tda = dta - dta[0]
with pytest.raises(NotImplementedError, match=msg):
tda.factorize(sort=True)

0 comments on commit 1dfcf11

Please sign in to comment.