【问题标题】:Error in due loop of ufunc does not support argument 0 of type str which has no callable log methodufunc 的正当循环中的错误不支持没有可调用日志方法的 str 类型的参数 0
【发布时间】:2021-02-14 13:08:20
【问题描述】:

当我使用以下日志功能时,我在我的 jupyter 笔记本中收到以下错误消息。 data1 = np.log(mdata).diff().dropna() 我试图做演员,但无法摆脱这个问题。 -------------------------------------------------- ------------------------- AttributeError Traceback(最近一次调用最后一次) AttributeError: 'str' 对象没有属性 'log'

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

TypeError                                 Traceback (most recent call last)
<ipython-input-13-b6f1878a7619> in <module>
----> 1 data1 = np.log(mdata).diff().dropna()

TypeError: loop of ufunc does not support argument 0 of type str which has no callable log method

数据:

          Field1    Field2  Field3  Field4
TS              
2020-11-02 00:43:58.500 1595000 849332  205 69
2020-11-02 00:43:58.500 1408000 820332  198 51
2020-11-02 00:43:58.500 1770000 926054  213 56
2020-11-02 00:43:58.500 1760000 1002332 216 72
2020-11-02 00:43:58.500 1850000 957054  213 59
... ... ... ... ...
 

mdata.index:

DatetimeIndex(['2020-11-02 00:43:58.500000', '2020-11-02 00:43:58.500000',
               '2020-11-02 00:43:58.500000', '2020-11-02 00:43:58.500000',
               '2020-11-02 00:43:58.500000', '2020-11-02 00:43:58.500000',
               '2020-11-02 00:43:58.500000', '2020-11-02 00:43:58.500000',
               '2020-11-02 00:43:58.600000', '2020-11-02 00:43:58.600000',
               ...

               '2020-11-02 00:44:00.400000', '2020-11-02 00:44:00.500000'],
              dtype='datetime64[ns]', name='TS', length=199, freq=None)

完整的代码是

df1 = pd.read_csv('inputdata.csv')
cols = ['Field1','Field2','Field3','Field4'] 
data_x = pd.to_datetime(data_x)
mdata = df1[cols]
mdata.index = pd.DatetimeIndex(data_x)
data1 = np.log(mdata).diff().dropna()

The following statements are not executed due to the issue in the above statement

model = VAR(data)
results = model.fit(2)
results.summary()

【问题讨论】:

  • 看起来您在 mdata 中的一个或多个列包含字符串。请发布mdata.info()的输出。
  • 谢谢。 mdata 包含字符串类型。请让我知道如何处理这个日志。 . DatetimeIndex: 199 个条目,2020-11-02 00:43:58.500000 到 2020-11-02 00:44:00.500000 数据列(共 4 列):# Column Non- Null Count Dtype --- ------ -------------- ----- 0 Field1 199 non-null object 1 Field2 199 non-null object 2 Field3 199 non-空对象 3 Field4 199 个非空对象 dtypes:object(4) 内存使用量:7.8+ KB
  • 当我将对象转换为浮点类型时它起作用了

标签: python time-series


【解决方案1】:

问题在于您的数据列不是数字类型,numpy.log() 需要数字数据。您可以使用pandas.to_numeric()将数据转换为数值

mdata = df1[cols].apply(pd.to_numeric)

或者通过使用DataFrame.astype()将列转换为特定的数字类型

mdata = df1[cols].astype(float)

【讨论】:

  • 使用 pd.to_numeric(df1[cols]) 时出现以下类型错误 TypeError: arg must be a list, tuple, 1-d array, or Series
  • @Karthika - 有多个列,我应该使用.apply()。它现在应该可以工作了。
猜你喜欢
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2020-12-09
相关资源
最近更新 更多