Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a pandas datetime index

I have a pandas datetime index that I construct. It may be that through the construction process that the individual members are not in order. I want to sort the index. Is there an obvious way I should be doing this?

What I've done so far is this

import pandas as pd
tseries = pd.to_datetime(['2001-02-04', '2013-08-14', '2008-01-25'])
print 'original unsorted tseries'
for t in tseries:
    print t
tseries.values.sort()
print 'new sorted tseries'
for t in tseries:
    print t

output:

original unsorted tseries
2001-02-04 00:00:00
2013-08-14 00:00:00
2008-01-25 00:00:00
new sorted tseries
2001-02-04 00:00:00
2008-01-25 00:00:00
2013-08-14 00:00:00

What is the appropriate way to do this? Is what I've done always expected to work?

like image 336
piRSquared Avatar asked Apr 10 '14 16:04

piRSquared


1 Answers

try

tseries.order()

If you work with a DataFrame, the term is called sort again, i.e. .sort() and .sort_index()

like image 101
Retozi Avatar answered Sep 19 '22 07:09

Retozi