from scipy import integrate import pandas as pd import numpy as np def integrate_method(self, how='trapz', unit='s'): '''Numerically integrate the time series. @param how: the method to use (trapz by default) @return Available methods: * trapz - trapezoidal * cumtrapz - cumulative trapezoidal * simps - Simpson's rule * romb - Romberger's rule See http://docs.scipy.org/doc/scipy/reference/integrate.html for the method details. or the source code https://github.com/scipy/scipy/blob/master/scipy/integrate/quadrature.py ''' available_rules = set(['trapz', 'cumtrapz', 'simps', 'romb']) if how in available_rules: rule = integrate.__getattribute__(how) else: print('Unsupported integration rule: %s' % (how)) print('Expecting one of these sample-based integration rules: %s' % (str(list(available_rules)))) raise AttributeError result = rule(self.values, self.index.astype(np.int64) / 10**9) #result = rule(self.values) return result pd.TimeSeries.integrate = integrate_method x = np.abs(np.random.randn(10000)) ts = pd.Series(x, pd.date_range(start='2013-05-03', periods=len(x), freq='s')) ts.integrate() ts.integrate('cumtrapz') ts.integrate('simps') ts[:1+2**3].integrate('romb') twos = pd.Series(2, pd.date_range(start='2012-01-23', periods=13, freq='1s')) twos.plot() print(twos.integrate('simps')) twos_sparse = pd.Series(2, pd.date_range(start='2012-01-23 00:00', end='2012-01-23 00:12', freq='3s')) twos_sparse.plot() print(twos_sparse.integrate('simps')) lin = pd.Series(np.arange(13), pd.date_range(start='2012-01-23', periods=13, freq='1s')) lin.plot() print(lin.integrate()) %timeit ts.integrate() ts.integrate('homers')