python - 从非周期性数据创建数据帧。 (重新调整时间戳)[Pandas Python]

标签 python datetime pandas

我有一些时间戳不规则的数据,如下所示:

                    Oil Pres.  Out ppm  Ratio   In  Out  Inlet psi  Bag psi  \
Date                                                                           
2013-12-10 19:01:00         56        0      0  455  462       2.26     0.44   
2013-12-10 20:00:00         55        0      0  450  456      10.91     0.73   
2013-12-10 20:33:00         55        0      0  443  452       9.24     0.54   
2013-12-10 22:00:00         54        0      0  440  449       8.16     0.62   
2013-12-10 22:31:00         54        0      0  436  445      10.29     0.49  

我如何对齐两个日期之间的数据(例如:2013-12-10 18:00:00 和 2013-12-11 00:00:00 以 20 分钟为间隔,使其显示如下:

                Oil Pres.  Out ppm  Ratio   In  Out  Inlet psi  Bag psi  \
Date               
2013-12-10 18:00:00         NaN...
2013-12-10 18:20:00                                                  
2013-12-10 18:40:00
2013-12-10 19:00:00         56        0      0  455  462       2.26     0.44   
2013-12-10 19:20:00
2013-12-10 19:40:00
2013-12-10 20:00:00         55        0      0  450  456      10.91     0.73   
2013-12-10 20:20:00
2013-12-10 20:40:00         55        0      0  443  452       9.24     0.54   
2013-12-10 21:00:00
2013-12-10 21:20:00
2013-12-10 21:40:00
2013-12-10 22:00:00         54        0      0  440  449       8.16     0.62   
2013-12-10 22:20:00
2013-12-10 22:40:00         54        0      0  436  445      10.29     0.49  
2013-12-10 23:00:00
2013-12-10 23:20:00
2013-12-10 23:40:00
2013-12-11 00:00:00

最佳答案

也许:

df.resample( '20T', how='mean' )

或者,您可以:

idx = pd.date_range( start='2013-12-10 18:00:00',
                     end='2013-12-11 00:00:00',
                     freq='20T' )
df.reindex( idx, method='ffill', limit=1 )

关于python - 从非周期性数据创建数据帧。 (重新调整时间戳)[Pandas Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20600526/

相关文章:

python - 为什么在使用 pandas.read_csv 后列名错误

python - 使用 pycurl 和端口号检索 url 的源代码?

python - 为什么 *x,y=[1,2,3,4] 合法时 *x=[1,2,3,4] 无效?

python - 如何根据带条件的列值在数据框中插入行?

python - 添加包含每个用户评分数的列,pandas

c# - DateTime 从 MMM-yyyy 转换为 dd-MM-yyyy 或 yyyy-MM-dd

python - 在Python中合并多个列表中的字典

python - innerhtml 相当于在 lxml.html 中使用 cssselect

python - '不再支持将列表喜欢传递给带有任何缺失标签的 .loc 或 [],请参阅

php - 比较来自 mysql 的 PHP 中的 2 个日期时间,以便在 android 中查看