pandas 如何按熊猫中的时间戳排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42462935/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:04:51  来源:igfitidea点击:

How to sort by timestamps in pandas?

pythonsortingpandasdatetimedataframe

提问by J Doe

So, I have timestamps that look like the following:

所以,我有如下所示的时间戳:

20140804:10:00:13.281486

20140804:10:00:13.400113

20140804:10:00:13.555512

20140804:10:00:13.435677

I have them in a DataFrame and I am trying to sort them in an ascending order. I have tried the following. But, it doesn't seem to work

我把它们放在一个 DataFrame 中,我试图按升序对它们进行排序。我尝试了以下方法。但是,它似乎不起作用

df['yyyymmdd'] = pd.to_numeric(df['yyyymmdd'], errors='coerce')

df['hh'] = pd.to_numeric(df['hh'], errors='coerce')

df['mm'] = pd.to_numeric(df['mm'], errors='coerce')

df['ss'] = pd.to_numeric(df['ss'], errors='coerce')

df=df.sort(['yyyymmdd', 'hh','mm','ss'], ascending=[True, True,True,True])

Any help is appreciated.

任何帮助表示赞赏。

回答by miradulo

You just have to ensure you denote the format specification properly, and you can use pd.to_datetimeto convert them to actualdatetimes before sort_values.

你只需要确保你表示格式规范正确,您可以使用pd.to_datetime将它们转换为实际的前日期时间sort_values

pd.to_datetime(stamps, format="%Y%m%d:%H:%M:%S.%f").sort_values()

This is much more direct than decomposing the timestamps in components and performing a multiple-criteria sort as you were attempting.

这比在您尝试时分解组件中的时间戳并执行多标准排序要直接得多。

Demo

演示

>>> stamps
0    20140804:10:00:13.281486
1    20140804:10:00:13.400113
2    20140804:10:00:13.555512
3    20140804:10:00:13.435677
dtype: object

>>> pd.to_datetime(stamps, format="%Y%m%d:%H:%M:%S.%f").sort_values()
0   2014-08-04 10:00:13.281486
1   2014-08-04 10:00:13.400113
3   2014-08-04 10:00:13.435677
2   2014-08-04 10:00:13.555512
dtype: datetime64[ns]