【问题标题】:What kind of TypeError is that?那是什么类型的错误?
【发布时间】:2021-05-19 06:19:05
【问题描述】:

有人理解这段代码的错误吗?最后一行代码好像有问题。

TypeError:/: 'str' 和 'str' 的操作数类型不受支持

BS = requests.get(f'https://financialmodelingprep.com/api/v3/balance-sheet-statement/{company}?apikey={demo}').json()

# get income statement as % of revenue for future predictions and forecast 5 next IS years
income_statement = pd.DataFrame.from_dict(IS[0], orient='index')
income_statement = income_statement[5:26]
income_statement.columns = ['current_year']
income_statement['as_%_of_revenue'] = income_statement /income_statement.iloc[0]

如果我更改代码

income_statement['as_%_of_revenue'] = income_statement /income_statement.iloc[0]

int,然后给我

 income_statement['as_%_of_revenue'] = int((income_statement) / int(income_statement)).iloc[0]
TypeError: int() argument must be a string, a bytes-like object or a number, not 'DataFrame'

【问题讨论】:

  • 请正确格式化您的代码并发布minimal reproducible example
  • 你想在最后一行实现什么?
  • 你能添加你的数据框的样子吗?
  • 嘿@PrakashDahal 是的,我会在下面解释为答案,因为它更清晰。非常感谢。

标签: python pandas typeerror


【解决方案1】:

虽然更多的研究可能已经解决了这个问题:

您试图将两个数字相除(我猜),但它们的格式为str。尝试将它们转换为可整除的东西,例如intfloat,简单示例:

**income_statement['as_%_of_revenue'] = int(income_statement) / int(income_statement.iloc[0]**)

更新

您似乎正在尝试将 pandas Dataframe 中的一行中的值除以该行中的另一个数字。如果你有数据框 df = pd.DataFrame(data=([100, 200], [500, 500])):

     0    1
0  100  200
1  500  500

您可以添加一列,其中包含第一列与第二列的百分比,如下所示:
df['as_%'] = (df[0] / df[1])*100。这导致:

     0    1   as_%
0  100  200   50.0
1  500  500  100.0

【讨论】:

  • 不幸的是没有解决问题..我之前尝试过
  • 你必须检查你试图划分的两个数字的类型。您的错误来自您试图将实际格式化为字符串的数字相除的事实。尝试使用type(income_statement)type(income_statement.iloc[0]**) 打印变量的类型,并确保它们不是str
  • 它是一个类,因为它的框架之前是:income_statement = pd.DataFrame.from_dict(IS[0], orient='index')
  • 在我看来,如果必须对第二列进行计算,这将起作用。不幸的是,无论如何我只有 1 列 [“当前年份”] 非常感谢您的帮助和时间,我很感激
【解决方案2】:

这条线不见了

net_income = IS[0]['netIncome']

这就是为什么“我想用最后一行实现的目标不清楚”的问题。因此整个代码是:

net_income = IS[0]['netIncome']

BS = requests.get(f'https://financialmodelingprep.com/api/v3/balance-sheet-statement/{company}?apikey={demo}').json()
    
    # get income statement as % of revenue for future predictions and forecast 5 next IS years
    income_statement = pd.DataFrame.from_dict(IS[0], orient='index')
    income_statement = income_statement[5:26]
    income_statement.columns = ['current_year']
    income_statement['as_%_of_revenue'] = income_statement /income_statement.iloc[0]

我想要实现的是将收入报表除以收入(销售额)= 收入/收入(均为当年)。结果将给我一个预测未来几年的收入百分比,它是一个乘数。

分割前的数据是这样的。

问题是income_statement被定义为一个类,因为dataframe

<class 'pandas.core.frame.DataFrame'>

                                 current_year
period                                     FY
revenue                             495308000
costOfRevenue                        43839000
grossProfit                         451469000
grossProfitRatio                     0.911491
researchAndDevelopmentExpenses      101117000
generalAndAdministrativeExpenses    101439000
sellingAndMarketingExpenses         252820000
otherExpenses                               0
operatingExpenses                   455376000
costAndExpenses                     499215000
interestExpense                      38119000
depreciationAndAmortization          12101000
ebitda                              8.194e+06
ebitdaratio                         0.0165432
operatingIncome                    -3.907e+06
operatingIncomeRatio              -0.00788802
totalOtherIncomeExpensesNet          14382000
incomeBeforeTax                     -27645000
incomeBeforeTaxRatio               -0.0558138
incomeTaxExpense                            0

我是 python 的新手(3 个月),所以我什至不确定所有简单的问题..我猜

感谢您的帮助

更新

我现在就是这样做的(甚至会花费我更多的时间)

我也申报了收入

net_income = IS[0]['netIncome']
revenue = IS[0]["revenue"]

并像这样计算

income_statement['as_%_of_revenue'] = (net_income/revenue)*100

期待这是否会导致任何进一步的问题,我猜

数据现在看起来像这样

                                 current_year  as_%_of_revenue
period                                     FY        -4.920978
revenue                             495308000        -4.920978
costOfRevenue                        43839000        -4.920978
grossProfit                         451469000        -4.920978
grossProfitRatio                     0.911491        -4.920978
researchAndDevelopmentExpenses      101117000        -4.920978
generalAndAdministrativeExpenses    101439000        -4.920978
sellingAndMarketingExpenses         252820000        -4.920978
otherExpenses                               0        -4.920978
operatingExpenses                   455376000        -4.920978
costAndExpenses                     499215000        -4.920978
interestExpense                      38119000        -4.920978
depreciationAndAmortization          12101000        -4.920978
ebitda                              8.194e+06        -4.920978
ebitdaratio                         0.0165432        -4.920978
operatingIncome                    -3.907e+06        -4.920978
operatingIncomeRatio              -0.00788802        -4.920978
totalOtherIncomeExpensesNet          14382000        -4.920978
incomeBeforeTax                     -27645000        -4.920978
incomeBeforeTaxRatio               -0.0558138        -4.920978
incomeTaxExpense                            0        -4.920978
netIncome                           -24374000        -4.920978

【讨论】:

  • 您还可以添加最终数据框的外观吗?
  • @PrakashDahal 不幸的是没有最终的数据框。它应该生成一个数字继续。最终数字应提交资产的公允价值。这里出现的唯一问题是,划分是基于一个对象(字典/类)
  • 我的意思是你的数据框应该根据你给出的最后一个代码包含新列"as_%_of_revenue",不是吗?我想看看该列的值是什么样的
  • 没错,但它不是...如果你的意思是这个代码:income_statement['as_%_of_revenue'] =income_statement /income_statement.iloc[0],这会出现 TypeError: unsupported operand type(s)对于/:'str'和'str'
  • 好吧,我只是想看看那列值可能是什么。你能计算一下并添加一个新列,那么你的问题就很容易理解了,否则我认为我没有理解那么多
【解决方案3】:

简单,你可以试试这个“income_statement =income_statement[6:26]”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多