Error in due loop of ufunc does not support argument 0 of type str which has no callable log method

Viewed 9316

When I use the below log function, i am getting the following error message in my jupyter notebook. data1 = np.log(mdata).diff().dropna() I tried to do cast but unable to get rid of this issue. --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) AttributeError: 'str' object has no attribute '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

mdata:

          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)

Complete code is

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()
1 Answers

The problem is that your data columns aren't a numeric type, and numpy.log() expects numeric data. You can convert the data to numeric values using pandas.to_numeric()

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

or by converting the columns to a specific numeric type by using DataFrame.astype()

mdata = df1[cols].astype(float)
Related