Peace Atlast
Peace Atlast

Reputation: 11

Pandas when dividing = TypeError: only integer scalar arrays can be converted to a scalar index

import yahoo_fin.stock_info as si
import pandas as pd
import numpy as np

x = si.get_balance_sheet('TD')

dfstock = pd.DataFrame(x)
dfstock = dfstock/10000000


dfstock = dfstock.fillna(0)


dfstock['Growth'] = dfstock['2020-10-31'] / dfstock['2017-10-31']

I am trying to divide two columns in my dataframe to get a third. I keep getting this error TypeError: only integer scalar arrays can be converted to a scalar index. I am not sure what to do, I have tried to fill in the blanks will 0 but it still does not work. Please help.

Upvotes: 0

Views: 106

Answers (1)

Henry Ecker
Henry Ecker

Reputation: 35676

yahoo_fin produces a DataFrame with a DatetimeIndex as columns. For this reason, we need to access the columns with a DateTime not a string. An easy way to do with is to convert the string with pd.to_datetime:

dfstock['Growth'] = (
        dfstock[pd.to_datetime('2020-10-31')] /
        dfstock[pd.to_datetime('2017-10-31')]
)

df_stock.head(5):

endDate                 2020-10-31 00:00:00  2019-10-31 00:00:00  2018-10-31 00:00:00  2017-10-31 00:00:00    Growth
Breakdown                                                                                                           
intangibleAssets                      212.5                250.3                245.9                261.8  0.811688
capitalSurplus                         12.1                 15.7                 19.3                 21.4  0.565421
totalLiab                          162036.6             132758.9             125486.3             120380.5  1.346037
totalStockholderEquity               8985.3               8190.7               7405.4               6946.4  1.293519
otherCurrentLiab                     5265.5               4436.1               5728.5               5627.5  0.935673

*Note the datetime columns


Alternatively we can convert the columns to strings using date and astype. Depending how we want to interact with the column:

dfstock.columns = dfstock.columns.date.astype(str)
dfstock['Growth'] = dfstock['2020-10-31'] / dfstock['2017-10-31']

df_stock.head(5):

                        2020-10-31  2019-10-31  2018-10-31  2017-10-31    Growth
Breakdown                                                                       
intangibleAssets             212.5       250.3       245.9       261.8  0.811688
capitalSurplus                12.1        15.7        19.3        21.4  0.565421
totalLiab                 162036.6    132758.9    125486.3    120380.5  1.346037
totalStockholderEquity      8985.3      8190.7      7405.4      6946.4  1.293519
otherCurrentLiab            5265.5      4436.1      5728.5      5627.5  0.935673

*Note the string column names


Setup used:

import pandas as pd
import yahoo_fin.stock_info as si

dfstock = si.get_balance_sheet('TD').div(10000000).fillna(0)

Upvotes: 1

Related Questions