简体   繁体   English

对pandas datetime索引进行排序

[英]Sort a pandas datetime index

I have a pandas datetime index that I construct. 我有一个构建的pandas datetime索引。 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? 我一直期望做的是什么?

try 尝试

tseries.order()

If you work with a DataFrame, the term is called sort again, ie .sort() and .sort_index() 如果使用DataFrame,则该术语再次被称为sort,即.sort()和.sort_index()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM