假设我有以下数据帧(时间序列中,第一塔是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。见下面的答案。 –