Sadik Hasan
Sadik Hasan

Reputation: 173

Using scipy to calculate integral over time series data without function

I have time-series data with y-values (around 6000 sample data) without the function in 13 minutes intervall in a csv file. For example: 2016-02-13 00:00:00 ; 0,353 2015-02-13 00:00:13 ; 0,362 ....

I want integrate over the range 9 and 14 o'clock. How I can read the values from csv to a np.array(data) ?

The Simpson approach integrates the whole curve under y. I have read in the doc. about my approach was:

from scipy.integrate import simps import numpy as np y = np.array(data) I = integrate.simps(y,dx=12) print(I)

How can i integrate in the range time (9am-2pm - 09:00:00-14:00:00) ?

Upvotes: 2

Views: 8110

Answers (1)

roadrunner66
roadrunner66

Reputation: 7941

I had a hard time handling the extra semicolon in your data with np.loadtxt, so here a handwritten import for your data.

%matplotlib inline
import numpy as np
import matplotlib.pyplot as p
from matplotlib.dates import strpdate2num, num2date
fn="example_data.txt"

#2016-02-13 00:00:00 ; 0,353
#2015-02-13 00:00:13 ; 0,362

def dateread(x):
    return num2date(strpdate2num('%Y-%m-%d %H:%M:%S')(x))

tims, vals=[],[]

f=open(fn,'r')
for line in f.readlines():
    #print line
    a=line.split()
    #print a
    time=dateread(a[0]+' '+a[1])
    value= float(a[3].replace(",","."))
    tims.append(time)
    vals.append(value)

print vals
print tims

p.plot(tims,vals)

Output:

[0.353, 0.362]
[datetime.datetime(2016, 2, 13, 0, 0, tzinfo=<matplotlib.dates._UTC object at 0x000000000411B4E0>), datetime.datetime(2015, 2, 13, 0, 0, 13, tzinfo=<matplotlib.dates._UTC object at 0x000000000411B4E0>)]

enter image description here

The times are now proper datetime objects, so they can be compared. To pick just the times you'd like see here : Python time comparison

Upvotes: 1

Related Questions