【问题标题】:PYTHON Error after nplog "TypeError: loop of ufunc does not support argument 0 of type int "nplog“TypeError:ufunc循环不支持int类型的参数0”后出现PYTHON错误
【发布时间】:2021-12-07 16:15:44
【问题描述】:

也许问题很简单,但我花了几个小时才找到问题,但找不到任何解决方案。

在第一部分中,我构建了一个从HERE 获取数据的数据框。我在提取我想要的东西时遇到了一些问题(英国每天累积的 covid 病例),但最终我设法得到了正确的形状。我的最终数据框大小为 1 列 x 约 600 行。

现在代码如下所示。

#BUILDING DATAFRAME

pd.set_option('display.max_rows', None)          
df = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
df=df[df["Country/Region"]=="United Kingdom"]
N = 11
df = df.iloc[N: , :]
df=df.drop(columns=["Province/State","Lat","Long","Country/Region"])
df = df.columns.to_frame().T.append(df, ignore_index=True)
df.columns = range(len(df.columns))
df=df.T
df = df.rename(columns={0: 'date', 1: 'nuovi_casi'})
df['nuovi_casi'] = df['nuovi_casi']+10000
M = 1
df = df.iloc[M: , :]
df=df.drop(columns=["date"]) 


#HERE IS THE PROBLEM

ts = df[['nuovi_casi']].dropna()
sts = ts.nuovi_casi
sts.index.name = None
ts_log = np.log(1+sts).dropna()

我必须添加行 df['nuovi_casi'] = df['nuovi_casi']+10000 因为我的数据框有一些 0 值。因此,我列出了所有值并使用另一个 Python 程序进行检查以确保现在我的数据框的所有值都高于 10000。

当我运行代码时,第二部分会引发如下错误:

AttributeError: 'int' object has no attribute 'log'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\\project_env\UK.py", line 69, in <module>
ts_log = np.log(1+sts).dropna()
File "C:\Users\\project_env\lib\site-packages\pandas\core\generic.py", line 
1933, in __array_ufunc__
return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)
File "C:\Users\\project_env\lib\site-packages\pandas\core\arraylike.py", line 
274, in array_ufunc
result = getattr(ufunc, method)(*inputs, **kwargs)
TypeError: loop of ufunc does not support argument 0 of type int which has no 
callable log method

然后我重新检查了所有值,但它们都是正数,并且值并不高,因为类似的代码(完美运行)可以处理超过 4500 万的数字,我不明白问题出在哪里。

你能找到解决办法吗?谢谢!

【问题讨论】:

  • 告诉我们sts。类型、形状、数据类型
  • 我猜sts 是一个对象 dtype 数组(或变成那个数组),它包含整数(除其他外)。在这种情况下,np.log 迭代数组元素并尝试调用大多数对象未实现的log 方法。 np.log 将适用于数字 dtype 数组,包括 int dtype,但不适用于 object dtype。
  • sts 类型是 使用另一个类似的程序,它可以完美运行(并且具有相同的类型)。在这里,构建数据框(我所做的方式)似乎对值类型进行了一些更改......
  • 看来,打印 sts 左侧有索引,我无法删除
  • 熊猫系列就是这样显示的。我不认为索引是问题

标签: python pandas dataframe numpy


【解决方案1】:

看来sts是熊猫系列

In [72]: import pandas as pd

log 适用于数字系列:

In [73]: ts = pd.Series([1,2,3])
In [74]: np.log(ts)
Out[74]: 
0    0.000000
1    0.693147
2    1.098612
dtype: float64

即使None 也不会打扰它

In [75]: ts = pd.Series([1,2,3,None])
In [76]: np.log(ts)
Out[76]: 
0    0.000000
1    0.693147
2    1.098612
3         NaN
dtype: float64

但是在Series中放一个字符串,你会得到一个对象dtype Series/array:

In [77]: ts = pd.Series([1,2,3,'astr'])
In [78]: np.log(ts)
AttributeError: 'int' object has no attribute 'log'

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "<ipython-input-78-b583cbb218e4>", line 1, in <module>
    np.log(ts)
  File "/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py", line 2032, in __array_ufunc__
    return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/pandas/core/arraylike.py", line 364, in array_ufunc
    result = getattr(ufunc, method)(*inputs, **kwargs)
TypeError: loop of ufunc does not support argument 0 of type int which has no callable log method

In [79]: ts
Out[79]: 
0       1
1       2
2       3
3    astr
dtype: object

【讨论】:

  • 如何将 sts 转换为正确的类型?问题是,如果我打印 sts ,它会给出一个数字系列(大约 600 个数值,左侧是从 1 到 600 的一种索引),所以由于某种原因,它可能无法将它们视为正确的类型。不明白。
  • 您可以在您的问题中添加sts.head 吗?
  • 我很抱歉,但我无法理解这个请求。我应该将 sts.head 添加到代码中吗?
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-12
相关资源
最近更新 更多