Skip to content

Commit f8e73d3

Browse files
committed
Merge pull request #3464 from jreback/GH3461
BUG: GH3461 Fix sorting in a frame with a list of columns which contains datetime64[ns]
2 parents 0bd5e77 + 2842383 commit f8e73d3

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pandas 0.12.0
5454
- ``.loc`` was not raising when passed an integer list (GH3449_)
5555
- Unordered time series selection was misbehaving when using label slicing (GH3448_)
5656
- Duplicate indexes with getitem will return items in the correct order (GH3455_, GH3457_)
57+
- Fix sorting in a frame with a list of columns which contains datetime64[ns] dtypes (GH3461_)
5758

5859
.. _GH3164: https://github.com/pydata/pandas/issues/3164
5960
.. _GH3251: https://github.com/pydata/pandas/issues/3251
@@ -64,6 +65,7 @@ pandas 0.12.0
6465
.. _GH3437: https://github.com/pydata/pandas/issues/3437
6566
.. _GH3455: https://github.com/pydata/pandas/issues/3455
6667
.. _GH3457: https://github.com/pydata/pandas/issues/3457
68+
.. _GH3461: https://github.com/pydata/pandas/issues/3461
6769
.. _GH3448: https://github.com/pydata/pandas/issues/3448
6870
.. _GH3449: https://github.com/pydata/pandas/issues/3449
6971

pandas/core/common.py

+2
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,8 @@ def is_timedelta64_dtype(arr_or_dtype):
14771477
tipo = arr_or_dtype.dtype.type
14781478
return issubclass(tipo, np.timedelta64)
14791479

1480+
def needs_i8_conversion(arr_or_dtype):
1481+
return is_datetime64_dtype(arr_or_dtype) or is_timedelta64_dtype(arr_or_dtype)
14801482

14811483
def is_float_dtype(arr_or_dtype):
14821484
if isinstance(arr_or_dtype, np.dtype):

pandas/core/frame.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,12 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False):
31443144
% str(x))
31453145
keys.append(k)
31463146

3147-
keys = [self[x].values for x in by]
3147+
def trans(v):
3148+
if com.needs_i8_conversion(v):
3149+
return v.view('i8')
3150+
return v
3151+
3152+
keys = [trans(self[x].values) for x in by]
31483153
indexer = _lexsort_indexer(keys, orders=ascending)
31493154
indexer = com._ensure_platform_int(indexer)
31503155
else:

pandas/tests/test_frame.py

+19
Original file line numberDiff line numberDiff line change
@@ -7737,6 +7737,25 @@ def test_sort_index_duplicates(self):
77377737
except Exception, e:
77387738
self.assertTrue('duplicate' in str(e))
77397739

7740+
def test_sort_datetimes(self):
7741+
7742+
# GH 3461, argsort / lexsort differences for a datetime column
7743+
df = DataFrame(['a','a','a','b','c','d','e','f','g'],
7744+
columns=['A'],
7745+
index=date_range('20130101',periods=9))
7746+
dts = [ Timestamp(x) for x in ['2004-02-11','2004-01-21','2004-01-26','2005-09-20','2010-10-04','2009-05-12','2008-11-12','2010-09-28','2010-09-28'] ]
7747+
df['B'] = dts[::2] + dts[1::2]
7748+
df['C'] = 2.
7749+
df['A1'] = 3.
7750+
7751+
df1 = df.sort(columns='A')
7752+
df2 = df.sort(columns=['A'])
7753+
assert_frame_equal(df1,df2)
7754+
7755+
df1 = df.sort(columns='B')
7756+
df2 = df.sort(columns=['B'])
7757+
assert_frame_equal(df1,df2)
7758+
77407759
def test_frame_column_inplace_sort_exception(self):
77417760
s = self.frame['A']
77427761
self.assertRaises(Exception, s.sort)

0 commit comments

Comments
 (0)