简体   繁体   English

一次将列值与另一次比较 pandas 日期时间索引

[英]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.

相关问题 将日期时间索引与日期时间列进行比较并更改另一列中的相应值 - Compare datetime index with a datetime column and change the corresponding value in another column Pandas 将一列中列表中的项目与另一列中的单个值进行比较 - Pandas compare items in list in one column with single value in another column 仅将datetime列与pandas中的时间进行比较 - compare a datetime column only to time in pandas 将 pandas 中的日期时间列与另一个日期时间列和返回索引匹配 - Matching datetime column in pandas with another datetime column and return index 熊猫:如果日期时间索引中的日期不同,则将一列的值添加到另一列 - Pandas: Adding values of one column to another column if the date is different in datetime index Pandas:将日期和时间加入一个日期时间列 - Pandas: Join Date and Time into one datetime column 将多个pandas数据帧列与另一个具有不同长度和索引的数据帧的一列进行比较 - compare multiple columns of pandas dataframe with one column of another dataframe having different length and index Pandas DateTime 索引,根据时间和日期过滤器为列分配值 - Pandas DateTime index, assign value to column based on based on time AND day filter 使用 Pandas 数据框将一列值与另一列中的其他元素列表进行比较 - using pandas dataframe compare one column value with other list of elements in another column 比较一列中的值是否在另一列python pandas中的两个值之间 - compare whether value in one column is between two values in another column python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM