假設我有以下數據幀(時間序列中,第一塔是DateTimeIndex)保留日期時間索引
atn file
datetime
2012-10-08 14:00:00 23.007462 1
2012-10-08 14:30:00 27.045666 1
2012-10-08 15:00:00 31.483825 1
2012-10-08 15:30:00 37.540651 2
2012-10-08 16:00:00 43.564573 2
2012-10-08 16:00:00 48.589852 2
2012-10-08 16:00:00 55.289452 2
我目標是向具有一定數目的在所述第一外觀提取行最後一欄「文件」,所以獲得與此表:
datetime atn
file
1 2012-10-08 14:00:00 23.007462
2 2012-10-08 15:30:00 37.540651
我方法是B組Y「文件」,然後聚集在「第一」:
dt.groupby(by="file").aggregate("first")
但是與此有關的問題是,則索引不被用作該分組的一列。我解決了這個首先通過添加索引的列:
dt2 = dt.reset_index()
dt2.groupby(by="file").aggregate("first")
但現在的問題是的datetime列不是日期了,但浮動:
datetime atn
file
1 1.349705e+18 23.007462
2 1.349710e+18 37.540651
有
- 將浮點數轉換回日期時間的方法?
- 或者一種保存groupby/aggregate-operation中日期時間的方法?
- 或更好的方式來實現這個最終tabel?
的示例數據幀可以被使用如下:
拷貝(到剪貼板):
2012-10-08 14:00:00, 23.007462, 1
2012-10-08 14:30:00, 27.045666, 1
2012-10-08 15:00:00, 31.483825, 1
2012-10-08 15:30:00, 37.540651, 2
2012-10-08 16:00:00, 43.564573, 2
2012-10-08 16:00:00, 48.589852, 2
2012-10-08 16:00:00, 55.289452, 2
然後:
dt = pandas.read_clipboard(sep=",", parse_dates=True, index_col=0,
names=["datetime", "atn", "file"])
您使用哪種版本的熊貓?在你的進程之後,我正在獲取'dt2'並且適當地保留了日期時間。 –
也許也很重要,我的numpy版本(datetime64相關的東西):>>> pandas .__ version__ '0.9.0' >>> np .__ version__ '1.6.1' – joris
好的。 'parse_dates'似乎是問題@joris。見下面的答案。 –