咱們有時候在對組數進行操做時候,偶爾會出現這個問題.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