Skip to content

Commit c6d55e2

Browse files
author
Jean-Mathieu Deschenes
committed
Fix for #16836
* 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 34210ac commit c6d55e2

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Bug Fixes
180180

181181
- Fixes regression in 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)
182182
- Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`)
183+
- Fixes regression when sorting by multiple columns on a datetime array with NaT values (:issue:`16836`)
183184

184185
Conversion
185186
^^^^^^^^^^

pandas/core/frame.py

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

3339-
def trans(v):
3340-
if needs_i8_conversion(v):
3341-
return v.view('i8')
3342-
return v
3343-
33443339
keys = []
33453340
for x in by:
33463341
k = self.xs(x, axis=other_axis).values
33473342
if k.ndim == 2:
33483343
raise ValueError('Cannot sort by duplicate column %s' %
33493344
str(x))
3350-
keys.append(trans(k))
3345+
keys.append(k)
33513346
indexer = lexsort_indexer(keys, orders=ascending,
33523347
na_position=na_position)
33533348
indexer = _ensure_platform_int(indexer)

pandas/tests/frame/test_sorting.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ 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',
95+
np.nan, '2016-01-01']]
96+
d2 = [Timestamp(x) for x in ['2017-01-01', '2014-01-01',
97+
'2016-01-01', '2015-01-01']]
98+
df = pd.DataFrame({'a': d1, 'b': d2}, index=[0, 1, 2, 3])
99+
100+
d3 = [Timestamp(x) for x in ['2015-01-01', '2016-01-01',
101+
'2016-01-01', np.nan]]
102+
d4 = [Timestamp(x) for x in ['2014-01-01', '2015-01-01',
103+
'2017-01-01', '2016-01-01']]
104+
expected = pd.DataFrame({'a': d3, 'b': d4}, index=[1, 3, 0, 2])
105+
sorted_df = df.sort_values(by=['a', 'b'], )
106+
tm.assert_frame_equal(sorted_df, expected)
107+
92108
def test_sort_values_inplace(self):
93109
frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4],
94110
columns=['A', 'B', 'C', 'D'])
@@ -269,6 +285,11 @@ def test_sort_datetimes(self):
269285
df2 = df.sort_values(by=['B'])
270286
assert_frame_equal(df1, df2)
271287

288+
df1 = df.sort_values(by='B')
289+
290+
df2 = df.sort_values(by=['C', 'B'])
291+
assert_frame_equal(df1, df2)
292+
272293
def test_frame_column_inplace_sort_exception(self):
273294
s = self.frame['A']
274295
with tm.assert_raises_regex(ValueError, "This Series is a view"):
@@ -321,7 +342,11 @@ def test_sort_nat_values_in_int_column(self):
321342
assert_frame_equal(df_sorted, df_reversed)
322343

323344
df_sorted = df.sort_values(["datetime", "float"], na_position="last")
324-
assert_frame_equal(df_sorted, df_reversed)
345+
assert_frame_equal(df_sorted, df)
346+
347+
# Ascending should not affect the results.
348+
df_sorted = df.sort_values(["datetime", "float"], ascending=False)
349+
assert_frame_equal(df_sorted, df)
325350

326351

327352
class TestDataFrameSortIndexKinds(TestData):

0 commit comments

Comments
 (0)