Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compute current integral over time in a pandas dataframe

I have a dataframe with columns speed and timestamp which is a simple range between 0 and 100. I would like to compute the following integral in a new column distance for each timestamp.

What I did :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import numpy as np
import pandas as pd

#some code ... 

dataframe.loc[:, "distance"] = [
     np.trapz(
          y=dataframe["speed"].iloc[:t], 
          x=dataframe["timestamp"].iloc[:t],
     )
     for t in range(0, dataframe.shape[0])
]

However, I do suspect there is more pythonic and efficient way to compute this integral, which better use the power and syntax of pandas, and so on.

Does someone have an idea ?

>Solution :

Probably you are looking for

dataframe["distance"] = dataframe["speed"].cumsum() * freq

where freq is the frequency of your time series (for example freq=10 if you have 10 records per second).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading