Skip to content

BUG: GH3461 Fix sorting in a frame with a list of columns which contains datetime64 #3464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2013
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
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pandas 0.12.0
- ``.loc`` was not raising when passed an integer list (GH3449_)
- Unordered time series selection was misbehaving when using label slicing (GH3448_)
- Duplicate indexes with getitem will return items in the correct order (GH3455_, GH3457_)
- Fix sorting in a frame with a list of columns which contains datetime64[ns] dtypes (GH3461_)

.. _GH3164: https://github.com/pydata/pandas/issues/3164
.. _GH3251: https://github.com/pydata/pandas/issues/3251
Expand All @@ -64,6 +65,7 @@ pandas 0.12.0
.. _GH3437: https://github.com/pydata/pandas/issues/3437
.. _GH3455: https://github.com/pydata/pandas/issues/3455
.. _GH3457: https://github.com/pydata/pandas/issues/3457
.. _GH3461: https://github.com/pydata/pandas/issues/3461
.. _GH3448: https://github.com/pydata/pandas/issues/3448
.. _GH3449: https://github.com/pydata/pandas/issues/3449

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,8 @@ def is_timedelta64_dtype(arr_or_dtype):
tipo = arr_or_dtype.dtype.type
return issubclass(tipo, np.timedelta64)

def needs_i8_conversion(arr_or_dtype):
return is_datetime64_dtype(arr_or_dtype) or is_timedelta64_dtype(arr_or_dtype)

def is_float_dtype(arr_or_dtype):
if isinstance(arr_or_dtype, np.dtype):
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,12 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False):
% str(x))
keys.append(k)

keys = [self[x].values for x in by]
def trans(v):
if com.needs_i8_conversion(v):
return v.view('i8')
return v

keys = [trans(self[x].values) for x in by]
indexer = _lexsort_indexer(keys, orders=ascending)
indexer = com._ensure_platform_int(indexer)
else:
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7737,6 +7737,25 @@ def test_sort_index_duplicates(self):
except Exception, e:
self.assertTrue('duplicate' in str(e))

def test_sort_datetimes(self):

# GH 3461, argsort / lexsort differences for a datetime column
df = DataFrame(['a','a','a','b','c','d','e','f','g'],
columns=['A'],
index=date_range('20130101',periods=9))
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'] ]
df['B'] = dts[::2] + dts[1::2]
df['C'] = 2.
df['A1'] = 3.

df1 = df.sort(columns='A')
df2 = df.sort(columns=['A'])
assert_frame_equal(df1,df2)

df1 = df.sort(columns='B')
df2 = df.sort(columns=['B'])
assert_frame_equal(df1,df2)

def test_frame_column_inplace_sort_exception(self):
s = self.frame['A']
self.assertRaises(Exception, s.sort)
Expand Down