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

比如:

#coding:utf-
import pandas as pd
import numpy as np if __name__ == '__main__':
np.random.seed()
df = pd.DataFrame( + np.random.randn().cumsum(), columns=['weight'])
df['pct_change'] = df.weight.pct_change()
df['w_log'] = np.log(np.asarray(df['weight']+ , dtype=object))
print df['w_log']

会出现这个问题:

   df['w_log'] = np.log(np.asarray(df['weight']+ , 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-
import pandas as pd
import numpy as np if __name__ == '__main__':
np.random.seed()
df = pd.DataFrame( + np.random.randn().cumsum(), columns=['weight'])
df['pct_change'] = df.weight.pct_change()
df['w_log'] = np.log(np.asarray(df['weight']+ , dtype=float))
print df['w_log']

将object对象,改成base类型就可以了.

结果:

     4.642120
4.645969
4.655321
4.676410
4.693652
4.684666
4.693403
4.692016
4.691069
4.694830
4.696146
4.709337
4.716171

完.

AttributeError: 'int' object has no attribute 'log'的更多相关文章

  1. AttributeError: 'int' object has no attribute 'isdigit'(python下的isdigit函数)

    python下的isdigit函数:  isdigit() 方法检测字符串是否只由数字组成. 语法 isdigit()方法语法:  str.isdigit() 示例代码如下: 结果: 我想说的重点在于 ...

  2. 解决代理池的问题AttributeError: 'int' object has no attribute 'items'

    https://blog.csdn.net/mygodit/article/details/86689127

  3. AttributeError: 'int' object has no attribute 'upper'

    因为安装的openpyxl版本是2.3.4,而代码是: sheet.cell(rownumber, 1).value = data['id']参数不对,应该是: sheet.cell(None, ro ...

  4. [已解决]报错:报错AttributeError: 'int' object has no attribute 'upper'

    原因:openpyxl版本低,需升级 pip install --upgrade openpyxl

  5. AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'

    /*************************************************************************** * AttributeError: 'modu ...

  6. 解决:pipenv shell报错:AttributeError: 'module' object has no attribute 'run'

    利用pipenv shell切换到虚拟环境时,显示报错:AttributeError: 'module' object has no attribute 'run' 可以看到是d:\program\p ...

  7. 测试类执行报错:AttributeError: 'Testlei' object has no attribute 'test_cases' 和data,unpack用法解析

    a=[{"}] import unittest from ddt import ddt,data,unpack @ddt class Testlei(unittest.TestCase): ...

  8. AttributeError: 'list' object has no attribute 'extends' && list详解

    拼写错误 是extend  而不是extends 出错demo: In [27]: c = [2,3] In [28]: c.extends([5]) ------------------------ ...

  9. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

随机推荐

  1. mysql版本升级

    环境 mysql安装在centos上,需要升级. mysql的版本是 mysql> select version(); +-----------+ | version() | +-------- ...

  2. ASP.NET Core MVC请求超时设置解决方案

    设置请求超时解决方案 当进行数据导入时,若导入数据比较大时此时在ASP.NET Core MVC会出现502 bad gateway请求超时情况(目前对于版本1.1有效,2.0未知),此时我们需要在项 ...

  3. iOS 类似2048、4096小游戏-OC

    大概思路(初步制作,粗工制造):demo 1.Collection 创建cell 2.cell上添加一个view,用来添加手势 3.字典用来存放数据->每次执行StarGame数组接收没有数字的 ...

  4. 搭建lnmp教程

    LNMP指的是一个基于CentOS/Debian 上安装Nginx.PHP.MySQL.php.可以在独立主机上轻松的安装LNMP生产环境. 1 安装nginx 如果是一台新的服务器可直接安装(若以前 ...

  5. async和enterproxy控制并发数量

    聊聊并发与并行 并发我们经常提及之,不管是web server,app并发无处不在,操作系统中,指一个时间段中几个程序处于已经启动运行到完毕之间,且这几个程序都是在同一处理机上运行,并且任一个时间点只 ...

  6. mysql 证明为什么用limit时,offset很大会影响性能

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/117 首先说明一下MySQL的版本: mysql> sel ...

  7. c#发送get请求

    c#发送get请求爬取网页 关键点:在控制台中发送一个get请求,将响应的内容写入文件流中保存html格式 static void Main(string[] args) { string url = ...

  8. java实现发送邮件服务器,SMTP协议发送邮件

    1.采用SMTP的邮件发送协议.准备:在网易注册一个邮箱,进入设置开启SMTP/pop3协议 2.接下来就是java代码实现了,下面都有注释,就不多做解释了. public class mail { ...

  9. template package (godoc 翻译)

    template 包 概述(Overview) template 包实现了数据驱动模板用于生成文本输出. 要生成HTML输出,请参阅html/template包,它具有与此包相同的接口,但会自动保护H ...

  10. C# DataGridVie利用model特性动态加载列

    今天闲来无事看到ORm的特性映射sql语句.我就想到datagridview也可以用这个来动态添加列.这样就不用每次都去界面上点开界面填列了. 代码简漏希望有人看到了能指点一二. 先定义好Datagr ...