[英]Compare column value at one time to another pandas datetime index
I have a pandas dataframe with a datetime index and some column, 'value'.我有一个 pandas dataframe 带有日期时间索引和一些列,“值”。 I would like to compare the 'value' value at a given time of day to the value at a different time of the same day.
我想将一天中给定时间的“值”值与同一天不同时间的值进行比较。 Eg compare the 10am value to the 10pm value.
例如,将上午 10 点的值与晚上 10 点的值进行比较。
Right now I can get the value at either side using:现在我可以使用以下方法获得任何一方的价值:
mask = df[(df.index.hour == hour)]
the problem is that this returns a dataframe indexed at hour.问题是这会返回一个在小时索引的 dataframe。 So doing mask1.value - mask2.value returns Nan's since the indexes are different.
所以做 mask1.value - mask2.value 返回 Nan's 因为索引不同。
I can get around this in a convoluted way:我可以用一种复杂的方式解决这个问题:
out = mask.value.loc["2020-07-15"].reset_index() - mask2.value.loc["2020-07-15"].reset_index() #assuming mask2 is the same as the mask call but at a different hour
but this is tiresome to loop over for a dataset that spans years.但是对于跨越数年的数据集来说循环是很烦人的。 (Obviously I could timedelta +=1 in the loop to avoid the hard calls).
(显然我可以在循环中使用 timedelta +=1 来避免硬调用)。
I don't actually care if some nan's get into the end result if some, eg 10am, values are missing.如果缺少某些值(例如上午 10 点),我实际上并不关心某些 nan 是否会进入最终结果。
Edit:编辑:
Initial dataframe:首字母 dataframe:
index values
2020-05-10T10:00:00 23
2020-05-10T11:00:00 20
2020-05-10T12:00:00 5
.....
2020-05-30T22:00:00 8
2020-05-30T23:00:00 8
2020-05-30T24:00:00 9
Expected dataframe:预计 dataframe:
index date newval
0 2020-05-10 18
.....
x 2020-05-30 1
where newval is some subtraction of the two different times I described above (eg. the 10am measurement - the 12pm measurement so 23-5 = 18), second entry is made up其中 newval 是我上面描述的两个不同时间的减法(例如上午 10 点测量 - 中午 12 点测量所以 23-5 = 18),第二个条目是弥补
it doesn't matter to me if date is a separate column or the index.日期是单独的列还是索引对我来说都没有关系。
A workaround:解决方法:
mask1 = df[(df.index.hour == hour1)]
mask2 = df[(df.index.hour == hour2)]
out = mask1.values - mask2.values # df.values returns an np array without indices
result_df = pd.DataFrame(index=pd.daterange(start,end), data=out)
It should save you the effort of looping over the dates它应该可以省去循环日期的工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.