Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: pct_change throws TypeError: unsupported operand type(s) for /: 'str' and 'float'

Tags:

python

pandas

I've imported into a DataFrame from JSON:

res = pd.io.json.json_normalize(response['candles'])

    complete    mid.c   mid.h   mid.l   mid.o   time    volume
3000    True    1.48257 1.48902 1.47545 1.48299 2011-05-02T21:00:00.000000000Z  46718
3001    True    1.48271 1.49402 1.47752 1.48254 2011-05-03T21:00:00.000000000Z  49927

Reordered columns and created new DataFrame and set DatetimeIndex:

...
newRes = res
newRes = newRes.set_index(pd.DatetimeIndex(newRes['time']))

time                            
2002-05-06 21:00:00 2002-05-06T21:00:00.000000000Z  0.91535 0.91535 0.91535 0.91535 1   True
2002-05-07 21:00:00 2002-05-07T21:00:00.000000000Z  0.90435 0.90435 0.90435 0.90435 1   True

Created another df

df = newRes[['mid.c']].copy()

time                mid.c
2002-05-06 21:00:00 0.91535
2002-05-07 21:00:00 0.90435

And now simple pct_change throws error.

df['rtns'] = df['mid.c'].pct_change(1)

TypeError: unsupported operand type(s) for /: 'str' and 'float'

Without parens I get

df['rtns'] = df['mid.c'].pct_change

time                mid.c   rtns    
2002-05-06 21:00:00 0.91535 <bound method NDFrame.pct_change of time\n2002...
2002-05-07 21:00:00 0.90435 <bound method NDFrame.pct_change of time\n2002...

I tried setting the datetime index as recommended in another post and it still didn't help (as shown here)

I've also tried to do the pct_change calculation manually by declaring float()s on all but that didn't work either.

Also tried .dropna

What am I doing wrong?

like image 395
cadig Avatar asked Oct 08 '17 22:10

cadig


1 Answers

Your column is a column of strings, so pct_change will throw an error. In the latter case, all you're doing is referencing the pct_change method, without actually calling the function itself.

You'll want to convert your column to float, and then compute the pct change.

r = df['mid.c'].astype(float).pct_change(1)

print(r)
3000         NaN
3001    0.000094
Name: mid.c, dtype: float64

df['rtns'] = r
like image 82
cs95 Avatar answered Nov 02 '22 13:11

cs95