Cyber_Tron
Cyber_Tron

Reputation: 299

Python Integral Function Compute Error in Dataframe

I'm trying to compute integral for Power field in my pandas dataframe against the timestamp field which looks like,

          timestamp        Power
0        1582848000007  8284.714925
1        1582848000016  8514.845083
2        1582848000026  8518.174961
3        1582848000036  8512.624938
4        1582848000046  8514.845083
5        1582848000056  8518.174961
6        1582848000066  8512.624938
7        1582848000076  8512.624938
8        1582848000086  8512.624938
9        1582848000096  8509.295060
10       1582848000107  8512.624938
11       1582848000116  8512.624938
12       1582848000136  8518.174961
13       1582848000146  8514.845083
14       1582848000156  8514.845083
15       1582848000166  8512.624938
16       1582848000176  8518.174961
17       1582848000186  8514.845083
18       1582848000207  8512.624938
19       1582848000216  8514.845083
20       1582848000226  8514.845083
21       1582848000236  8514.845083
22       1582848000246  8512.624938
23       1582848000256  8514.845083
24       1582848000266  8514.845083
25       1582848000276  8514.845083
26       1582848000286  8518.174961
27       1582848000296  8512.624938
28       1582848000307  8514.845083
29       1582848000316  8131.295064

This is stored in variable df.

The code i'm using is the trapezoidal rule from scipy,

from scipy import integrate
df.apply(lambda g: integrate.trapz(g.Power, x=g.timestamp))

The error I get is,

Traceback (most recent call last):
  File "presto_run.py", line 17, in <module>
    I2=df1.apply(lambda g: integrate.trapz(g.Power, x=g.timestamp))
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 6487, in apply
    return op.get_result()
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/apply.py", line 151, in get_result
    return self.apply_standard()
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/apply.py", line 257, in apply_standard
    self.apply_series_generator()
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/apply.py", line 286, in apply_series_generator
    results[i] = self.f(v)
  File "presto_run.py", line 17, in <lambda>
    I2=df1.apply(lambda g: integrate.trapz(g.Power, x=g.timestamp))
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 5067, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: ("'Series' object has no attribute 'Power'", u'occurred at index timestamp')

What am I doing wrong? TIA!

Upvotes: 0

Views: 55

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

The syntax is integrate.trapz(y,x=None) where y and x are array-like. So you want to do:

integrate.trapz(df.Power, x=df.timestamp)
# out: 2628206.221949

Also there's an identical function from numpy:

np.trapz(df.Power, x=df.timestamp)
# out: 2628206.221949

Upvotes: 1

Related Questions