比较不同日期的数据 [英] Comparing data across dates at different times

查看:119
本文介绍了比较不同日期的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datetime dataFrame中有一系列数据,需要比较一个日期和时间与前一天的另一时间的数据。在下面的示例中,例如,我需要计算从2016-11-09 09:30:00到2016-11-10 21:30:00的百分比变化(本质上是从t @ 9:30到表中的next_date @ 21:30。)不幸的是,简单的滞后功能可能无法正常工作,因为记录的模式是不可预测的,并且所需的行之间可能没有相同数量的记录。

I have a sequence of data in datetime dataFrame and need to compare the data from one date and time to the prior day's another time. In the example below, I would, for example need to calculate percentage change from 2016-11-09 09:30:00 to 2016-11-10 21:30:00 (essentially from t @ 9:30 to next_date in the table @ 21:30.) Unfortunately a simple lag function might not work, since the pattern of the records are unpredictable and there might not be the same number of records between the desired rows.

dates = pd.date_range('2016-11-09 09:30:00',periods=10, freq='12H')
df =pd.DataFrame(np.random.randn(10,4)*100,index=dates,columns=list('ABCD'))

                              A|           B|           C|           D
-----------------------------------------------------------------------
2016-11-09 09:30:00|   74.409062|    3.635309|   17.603051|    6.743699

2016-11-09 21:30:00|   25.707464|  133.592600| -176.460798|  236.354740

2016-11-10 09:30:00|  -13.035709|  -82.974810|  106.204290|  -31.382023

2016-11-10 21:30:00| -120.712954|   -2.636682|   16.839875|  -12.177463

2016-11-11 09:30:00| -195.382169| -102.214945|   84.151532| -130.732630

....

是否有一种简单的方法这种比较还是我需要运行for循环?

Is there an easy way to do this comparison or would I need to run a for loop?

推荐答案

我认为最好的方法可能是将两组将两个不同时间的数据记录到两个DataFrame中,对齐日期,摆脱时间并在两个DataFrame中进行比较。

I think the best approach might be to split the two set of records at the two different times into two DataFrames, align the dates, get rid of the times and compare them across the two DataFrames.

这篇关于比较不同日期的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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