Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def test_time_integrate(self):

pd.testing.assert_series_equal(ref, time_integrate(test) / 3600)

test = pd.DataFrame(
{
"cpt1": [],
"cpt2": [],
},
index=pd.date_range("2009-01-01 00:00:00", freq="10s", periods=0, tz="UTC"),
)

ref = pd.Series(index=test.columns, dtype=float)

pd.testing.assert_series_equal(ref, time_integrate(test))

def test_aggregate_time_series(self):
sim_res = pd.DataFrame(
{"a": [1, 2], "b": [3, 4]},
Expand Down
17 changes: 8 additions & 9 deletions tide/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pandas as pd
import numpy as np

from scipy import integrate
from collections.abc import Callable

from tide.utils import check_and_return_dt_index_df
Expand Down Expand Up @@ -80,8 +79,8 @@ def time_gradient(data: pd.DataFrame | pd.Series) -> pd.DataFrame:

def time_integrate(data: pd.DataFrame | pd.Series) -> pd.Series:
"""
Perform time Integration of given time series in X DartaFrame or in a Series.
The function computes the integral of each column using `scipy.integrate.trapz`
Perform time Integration of given time series in X DataFrame or in a Series.
The function computes the integral of each column using `np.trapz`
function and the time difference between consecutive data points.

Parameters:
Expand All @@ -92,14 +91,14 @@ def time_integrate(data: pd.DataFrame | pd.Series) -> pd.Series:
"""

data = check_and_return_dt_index_df(data)
chrono = (data.index - data.index[0]).to_series()
chrono = chrono.dt.total_seconds()
if data.empty:
return pd.Series(index=data.columns, dtype=float)

res_series = pd.Series(dtype="float64")
for col in data:
res_series[col] = integrate.trapezoid(data[col], chrono)
t = (data.index.view("int64") - data.index[0].value) * 1e-9 # seconds
y = data.to_numpy()
result = np.trapz(y, t, axis=0)

return res_series
return pd.Series(result, index=data.columns)


def aggregate_time_series(
Expand Down
Loading