With two tables, Values
and dates
, I would like to get the average value between the date ranges.
Values
looks like:
Date | Value |
---|---|
2023-01-01 10:00 | 1 |
2023-01-01 11:00 | 2 |
2023-01-02 10:00 | 4 |
2023-01-04 10:00 | 4 |
2023-01-07 10:00 | 4 |
and dates
looks like
Group | StartDay | EndDay |
---|---|---|
1 | 2023-01-01 | 2023-01-05 |
2 | 2023-01-03 | 2023-01-10 |
As you can see, the date ranges can overlap.
I am trying to calculate the averages over these ranges, so in this example the output should be something along the lines of
Group | StartDay | EndDay | Mean |
---|---|---|---|
1 | 2023-01-01 | 2023-01-05 | 2.75 |
2 | 2023-01-03 | 2023-01-10 | 4 |
Currently my code looks like (all one line):
Values.groupby(np.where(Values['Date'].between(Dates['StartDay'],Dates['EndDay']),'pre','post'))['value'].mean()
however this results in
ValueError: Can only compare identically-labeled Series objects
This was based on other similar questions, however does not appear to apply here due to it being over two tables / using ranges.