将Pandas数据框转换为时间序列 [英] Convert Pandas dataframe to time series

查看:209
本文介绍了将Pandas数据框转换为时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pandas DataFrame:

Out[57]: 
      lastrun           rate
0   2013-11-04 12:15:02   0
1   2013-11-04 13:14:50   4
2   2013-11-04 14:14:48   10
3   2013-11-04 16:14:59   16

我想将其转换为每小时的时间序列并内插缺失值(15:00),以便最终得到:

2013-11-04 12:00:00   0
2013-11-04 13:00:00   4
2013-11-04 14:00:00   10
2013-11-04 15:00:00   13
2013-11-04 16:00:00   16

如何在熊猫中将数据框数据转换/映射到时间序列?

解决方案

假设您的"lastrun"具有日期时间对象:

In [22]: s = df.set_index('lastrun').resample('H')['rate']
In [23]: s
Out[23]: 
lastrun
2013-11-04 12:00:00     0
2013-11-04 13:00:00     4
2013-11-04 14:00:00    10
2013-11-04 15:00:00   NaN
2013-11-04 16:00:00    16
Freq: H, dtype: float64

In [24]: s.interpolate()
Out[24]: 
lastrun
2013-11-04 12:00:00     0
2013-11-04 13:00:00     4
2013-11-04 14:00:00    10
2013-11-04 15:00:00    13
2013-11-04 16:00:00    16
Freq: H, dtype: int64

那就是如果要线性插值.在即将发布的.13版本中,有一个提供更多选项! /p>

I have a Pandas DataFrame:

Out[57]: 
      lastrun           rate
0   2013-11-04 12:15:02   0
1   2013-11-04 13:14:50   4
2   2013-11-04 14:14:48   10
3   2013-11-04 16:14:59   16

I would like to convert that into an hourly time series and interpolate missing values (15:00) so that I end up with:

2013-11-04 12:00:00   0
2013-11-04 13:00:00   4
2013-11-04 14:00:00   10
2013-11-04 15:00:00   13
2013-11-04 16:00:00   16

How do I convert / map the dataframe data to a time series in Pandas?

解决方案

Assuming your 'lastrun' has datetime objects:

In [22]: s = df.set_index('lastrun').resample('H')['rate']
In [23]: s
Out[23]: 
lastrun
2013-11-04 12:00:00     0
2013-11-04 13:00:00     4
2013-11-04 14:00:00    10
2013-11-04 15:00:00   NaN
2013-11-04 16:00:00    16
Freq: H, dtype: float64

In [24]: s.interpolate()
Out[24]: 
lastrun
2013-11-04 12:00:00     0
2013-11-04 13:00:00     4
2013-11-04 14:00:00    10
2013-11-04 15:00:00    13
2013-11-04 16:00:00    16
Freq: H, dtype: int64

That's if you want linear interpolation. There's a bunch more options in the upcoming .13 release!

这篇关于将Pandas数据框转换为时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆