2016-07-14 126 views
2

我正在嘗試基於數據類型爲datetime64 [ns]的DateTime字段對數據幀進行排序。根據日期時間字段對熊貓數據幀排序

我的數據框看起來是這樣的:

Name DateTime1 
P38  NaT 
P62  2016-07-13 16:03:32.771 
P59  2016-06-23 14:23:42.461 
P07  NaT 
P16  2016-06-23 14:02:06.237 
P06  2016-07-13 16:03:52.570 
P106 2016-07-13 19:56:22.676 

當我排序它使用DateTime字段,

df.sort_values(by='DateTime1',ascending=True) 

我沒有得到期望的結果。

輸出:

Name DateTime1 
P16  2016-06-23 14:02:06.237 
P59  2016-06-23 14:23:42.461 
P62  2016-07-13 16:03:32.771 
P06  2016-07-13 16:03:52.570 
P106 2016-07-13 19:56:22.676 
P38  NaT 
P07  NaT 

回答

3

您的代碼應該工作?

嘗試

df = df.sort_values(by='DateTime1',ascending=True) 
df 

你可以創建一個索引

df.set_index('DateTime1', drop=True, append=False, inplace=True, verify_integrity=False) 
df = df.sort_index()