2

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) ?

Sadik Hasan
  • 173
  • 3
  • 18

1 Answers1

1

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

Community
  • 1
  • 1
roadrunner66
  • 7,772
  • 4
  • 32
  • 38
  • ok but simps(y,x) causes exception. y are the values and x the timestamps – Sadik Hasan Apr 14 '16 at 18:48
  • 1
    The proper reading of the timestamps was needed so you can pick and chose the times. For the integration I would set the first timestamp to time 0 and count the times in seconds. So the vector you turn over to the integration should be `real` on the x-axis. You'll be able to find plenty of answers on how to convert `datetime` to `seconds` on SO, e.g here: http://stackoverflow.com/questions/7852855/how-to-convert-a-python-datetime-object-to-seconds – roadrunner66 Apr 14 '16 at 19:54