1

I want to use cumtrapz on a pd.DataFrame that looks something like this:

               TIME    VALUE
2020-01-01 12:00:00       10
2020-01-01 12:00:01      -10
2020-01-01 12:02:20        2
2020-01-01 12:10:00        3
                ...      ...

Note that the gaps between the times are irregular, which is why I opt to choose cumtrapz.

Is there a possibility to calculate the negative and positive areas separately? See the image below, I need to calculate + and - and ultimately divide one by the other to receive the relation of positive to negative area.

integration areas

I know that I could split the pd.DataFrame into two separate ones, replacing the positive values with 0 in one of them and the negative values with 0 in within the other. Subsequently, I could run cumtrapz on both of them. However, I'm looking for a more accurate method.

  • 2
    Short of rolling your own trapezoid function, I would go with your suggestion of splitting into two dataframes. You can use `df.clip(lower=0)` and `df.clip(upper=0)` to cleanly clear positive/negative values. – ddg Jun 15 '21 at 15:04
  • 1
    How is it less accurate if you split it into two dataframes? – RMRiver Jun 15 '21 at 15:04
  • 1
    You do miss the trapezoids over positive/negative junctions. A jump from 3 to -1 and a jump from 1 to -1 would be treated identically when splitting. – ddg Jun 15 '21 at 15:05
  • 1
    If you really want the accuracy, go to the source code (https://github.com/scipy/scipy/blob/master/scipy/integrate/_quadrature.py), copy the function, and modify it to do what you want. I think you'll want something right before the summation on line 377. – ddg Jun 15 '21 at 15:12

0 Answers0