Skip to content

Commit 41466b0

Browse files
author
Jean-Mathieu Deschenes
committed
* Removed unnecessary conversion to i8 * Fixed failed test (`test_frame_column_inplace_sort_exception`) * Added check to ensure that the test is performing its intended goal(`test_sort_nan`)
1 parent 4ca9fcd commit 41466b0

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Bug Fixes
144144
~~~~~~~~~
145145

146146
- Fixes regression in 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)
147+
- Fixes regression when sorting by multiple columns on a datetime array with NaT values (:issue:`16836`)
147148

148149
Conversion
149150
^^^^^^^^^^

pandas/core/frame.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -3303,18 +3303,13 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False,
33033303
if len(by) > 1:
33043304
from pandas.core.sorting import lexsort_indexer
33053305

3306-
def trans(v):
3307-
if needs_i8_conversion(v):
3308-
return v.view('i8')
3309-
return v
3310-
33113306
keys = []
33123307
for x in by:
33133308
k = self.xs(x, axis=other_axis).values
33143309
if k.ndim == 2:
33153310
raise ValueError('Cannot sort by duplicate column %s' %
33163311
str(x))
3317-
keys.append(trans(k))
3312+
keys.append(k)
33183313
indexer = lexsort_indexer(keys, orders=ascending,
33193314
na_position=na_position)
33203315
indexer = _ensure_platform_int(indexer)

pandas/tests/frame/test_sorting.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ def test_sort_values(self):
8989
with tm.assert_raises_regex(ValueError, msg):
9090
frame.sort_values(by=['A', 'B'], axis=0, ascending=[True] * 5)
9191

92+
# GH 16836
93+
94+
d1 = [Timestamp(x) for x in ['2016-01-01', '2015-01-01', np.nan, '2016-01-01']]
95+
d2 = [Timestamp(x) for x in ['2017-01-01', '2014-01-01', '2016-01-01', '2015-01-01']]
96+
df = pd.DataFrame({'a': d1, 'b': d2}, index=[0, 1, 2, 3])
97+
98+
d3 = [Timestamp(x) for x in ['2015-01-01', '2016-01-01', '2016-01-01', np.nan]]
99+
d4 = [Timestamp(x) for x in ['2014-01-01', '2015-01-01', '2017-01-01', '2016-01-01']]
100+
expected = pd.DataFrame({'a': d3, 'b': d4}, index=[1, 3, 0, 2])
101+
sorted_df = df.sort_values(by=['a', 'b'], )
102+
tm.assert_frame_equal(sorted_df, expected)
103+
92104
def test_sort_values_inplace(self):
93105
frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4],
94106
columns=['A', 'B', 'C', 'D'])
@@ -269,6 +281,11 @@ def test_sort_datetimes(self):
269281
df2 = df.sort_values(by=['B'])
270282
assert_frame_equal(df1, df2)
271283

284+
df1 = df.sort_values(by='B')
285+
286+
df2 = df.sort_values(by=['C', 'B'])
287+
assert_frame_equal(df1, df2)
288+
272289
def test_frame_column_inplace_sort_exception(self):
273290
s = self.frame['A']
274291
with tm.assert_raises_regex(ValueError, "This Series is a view"):
@@ -321,7 +338,11 @@ def test_sort_nat_values_in_int_column(self):
321338
assert_frame_equal(df_sorted, df_reversed)
322339

323340
df_sorted = df.sort_values(["datetime", "float"], na_position="last")
324-
assert_frame_equal(df_sorted, df_reversed)
341+
assert_frame_equal(df_sorted, df)
342+
343+
# Ascending should not affect the results.
344+
df_sorted = df.sort_values(["datetime", "float"], ascending=False)
345+
assert_frame_equal(df_sorted, df)
325346

326347

327348
class TestDataFrameSortIndexKinds(TestData):

0 commit comments

Comments
 (0)