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

咱们有时候在对组数进行操做时候,偶尔会出现这个问题.dom

好比:spa

#coding:utf-8
import pandas as pd
import numpy as np

if __name__ == '__main__':
    np.random.seed(0)
    df = pd.DataFrame(100 + np.random.randn(100).cumsum(), columns=['weight'])
    df['pct_change'] = df.weight.pct_change()
    df['w_log'] = np.log(np.asarray(df['weight']+2 , dtype=object))
    print df['w_log']

会出现这个问题:code

   df['w_log'] = np.log(np.asarray(df['weight']+2 , dtype=object))
AttributeError: 'float' object has no attribute 'log'

这个问题的缘由是object没有log操做:上述操做等同于对象

那么咱们该怎么样来修正呢?np.log(np.array([x], dtype=object)) <-> np.array([x.log()], dtype=object)

#coding:utf-8
import pandas as pd
import numpy as np

if __name__ == '__main__':
    np.random.seed(0)
    df = pd.DataFrame(100 + np.random.randn(100).cumsum(), columns=['weight'])
    df['pct_change'] = df.weight.pct_change()
    df['w_log'] = np.log(np.asarray(df['weight']+2 , dtype=float))
    print df['w_log']

将object对象,改为base类型就能够了. blog

结果:pandas

0     4.642120
1     4.645969
2     4.655321
3     4.676410
4     4.693652
5     4.684666
6     4.693403
7     4.692016
8     4.691069
9     4.694830
10    4.696146
11    4.709337
12    4.716171

完.class

相关文章
相关标签/搜索