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

比如:

#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. OC学习16——对象归档

    转载自  OC学习篇之---归档和解挡 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOutputStream来进行操作的.当然在操作的这些对象都 ...

  2. 页面重绘(repaint)和回流(reflow)

    前言 页面显示到浏览器上的过程: 1.1.生成一个DOM树. 浏览器将获取到的HTML代码解析成1个DOM树,包含了所有标签,包括display:none和动态添加的节点. 1.2.生成样式结构体. ...

  3. java获取当前应用的运行信息(内存,线程,运行时间,状态等)

    一:目的 写这一段程序的原因是需要监控部署的的应用是否正常运行,并且显示其运行状态.在进程莫名死掉后甚至可以自动启动该应用. 首先这段代码可以获取的信息如下 /** * 当前进程运行的主机名 */ p ...

  4. DeepLearning.ai学习笔记(四)卷积神经网络 -- week1 卷积神经网络基础知识介绍

    一.计算机视觉 如图示,之前课程中介绍的都是64* 64 3的图像,而一旦图像质量增加,例如变成1000 1000 * 3的时候那么此时的神经网络的计算量会巨大,显然这不现实.所以需要引入其他的方法来 ...

  5. NP完整性| 集1(简介)

    我们一直在写关于高效算法来解决复杂问题,如最短路径,欧拉图,最小生成树等.这些都是算法设计者的成功故事. 在这篇文章中,讨论了计算机科学的失败故事. 计算机可以解决所有的计算问题吗? 存在计算问题,即 ...

  6. bzoj 3571: [Hnoi2014]画框

    Description 小T准备在家里摆放几幅画,为此他买来了N幅画和N个画框.为了体现他的品味,小T希望能合理地搭配画与画框,使得其显得既不过于平庸也不太违和.对于第 幅画与第 个画框的配对,小T都 ...

  7. lesson - 8 课程笔记 tar / gzip /bzip2 / xz /

    作用:为linux的文件和目录创建档案,也可以在档案中改变文件,或者向档案中加入新的文件即用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的  语法:tar[必要参数][选择参数][ ...

  8. ValueError: 'format' in __slots__ conflicts with class variable

    Complete output from command python setup.py egg_info: Traceback (most recent call last): File " ...

  9. 全国交通咨询系统 by C++ on Linux

    信息存储 利用邻接表存储城市信息与线路信息,比邻接矩阵更加高效. 主要数据结构 I)Time,规范时间的输入输出格式 II)VNode,头结点,用于建立顶点表,存储城市信息 III)ArcNode,表 ...

  10. Kafka的基本概念与安装指南(单机+集群同步)

    最近在搞spark streaming,很自然的前端对接的就是kafka.不过在kafka的使用中还是遇到一些问题,比如mirrormaker莫名其妙的丢失数据[原因稍后再说],消费数据offset错 ...