2015-10-26 20 views
0

我在熊貓中使用DataFrame來分析數據。樣本:如何從Python中的熊貓中的一系列有序數字中生成時間序列

data[:5] 
     time qlen  means  vars 
1 1.153281  1 0.000000 0.000000 
2 5.279293  1 0.333333 0.222222 
3 12.285338  1 0.400000 0.240000 
4 16.407872  1 0.428571 0.244898 
5 23.184910  1 0.444444 0.246914 

與「時間」列以秒爲單位。

如何將'時間'的浮點值轉換爲實際時間序列?

我試過pandas.date_range,但找不到合適的方法,主要是因爲時間點不會以相等的間隔發生。

+0

你可以添加基準日期到'時間'嗎? – Zero

回答

0

如果您希望將時間作爲timedelta(即無日期)類型,請使用to_timedelta轉換函數指定單位。

In [11]: pd.to_timedelta(df['time'], unit='s') 
Out[11]: 
1 00:00:01.153281 
2 00:00:05.279293 
3 00:00:12.285338 
4 00:00:16.407872 
5 00:00:23.184910 
Name: time, dtype: timedelta64[ns] 

如果您想要特定日期的時間,只需將增量添加到它。

In [12]: pd.to_timedelta(df['time'], unit='s') + pd.Timestamp('2014-01-01') 
Out[12]: 
1 2014-01-01 00:00:01.153281 
2 2014-01-01 00:00:05.279293 
3 2014-01-01 00:00:12.285338 
4 2014-01-01 00:00:16.407872 
5 2014-01-01 00:00:23.184910 
Name: time, dtype: datetime64[ns] 
相關問題