pandas 对熊猫日期时间索引进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22993603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort a pandas datetime index
提问by piRSquared
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?
我有一个我构建的Pandas日期时间索引。可能是通过构建过程,个别成员不正常。我想对索引进行排序。有没有明显的方法我应该这样做?
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?
这样做的合适方法是什么?我所做的总是预期有效吗?
回答by Retozi
try
尝试
tseries.order()
If you work with a DataFrame, the term is called sort again, i.e. .sort() and .sort_index()
如果您使用 DataFrame,则该术语再次称为排序,即 .sort() 和 .sort_index()

