简体   繁体   中英

Calculating pandas pct_change when df contains string getting error: TypeError: unsupported operand type(s) for /: 'float' and 'str'

My dataframe looks like this:

           ticker     close_x     close_y
date                                     
2020-06-02   SPOT  183.429993  189.229996
2020-06-03   SPOT  189.289993  184.639999
2020-06-04   SPOT  183.910004  181.830002
2020-06-05   SPOT  177.770004  184.059998
2020-06-08   SPOT  183.690002  187.330002
2020-06-02   TSLA  892.288025  881.400024
2020-06-03   TSLA  885.739929  882.510010

Here is my code which generates the above:

df['date'] = pd.to_datetime(df['created_at']).dt.date
df['time'] = pd.to_datetime(df['created_at']).dt.time
df.sort_values(by=['ticker','date', 'time'], inplace=True)
first = df.groupby(['ticker','date']).first()['close']
df_first = first.to_frame().reset_index()
last = df.groupby(['ticker','date']).last()['close']
df_last = last.to_frame().reset_index()
df_merged = df_first.merge(df_last, left_on=['ticker','date'], right_on=['ticker','date'])
df_merged.set_index(['date'], inplace=True)

I want to calculate the pct_change between close_x and close_y . When I try

df_merged['pct_change'] = df_merged.pct_change(axis=1)['close_y']

I get this error:

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

due to the ticker column being string. How do I resolve this?

You can select only columns close s:

df_merged['pct_change'] = df_merged[['close_x','close_y']].pct_change(axis=1)['close_y']
print (df_merged)
           ticker     close_x     close_y  pct_change
date                                                 
2020-06-02   SPOT  183.429993  189.229996    0.031620
2020-06-03   SPOT  189.289993  184.639999   -0.024565
2020-06-04   SPOT  183.910004  181.830002   -0.011310
2020-06-05   SPOT  177.770004  184.059998    0.035383
2020-06-08   SPOT  183.690002  187.330002    0.019816
2020-06-02   TSLA  892.288025  881.400024   -0.012202
2020-06-03   TSLA  885.739929  882.510010   -0.003647

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM