美文网首页
20220105 Python Debug

20220105 Python Debug

作者: hydro | 来源:发表于2022-01-17 06:50 被阅读0次


debug  np.array 不支持每个元素字符串使用  +  直接拼合


---> 60 stockListName2= stockListName+ stockListNameNumber

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U4'), dtype('<U4')) -> dtype('<U4')

出错原因:一个小端编码的 unicode 字符串,最多 9 个字符。


debug   AttributeError: 'DataFrame' object has no attribute 'reshape'

属性错误:“DataFrame”对象没有属性“reshape”

解决方法   “DataFrame”对象没有属性“reshape”,但是DataFrame.values有该方法!    

DataFrame.values.reshape(-1, 1)

原文链接:https://blog.csdn.net/qq_41185868/article/details/88643276



debug

iplot 中,坐标轴不按升序排列,而是乱排。原因是传入的数据类型是string,应该是float


debug 缩小某一列数据


## 开始可行  

for i in range(len(array5)):

    array5[i][5]=array5[i][5].astype(np.float32)/100000

##下面是 报错的代码,说 str没有 astype 属性

for i in range(len(array3)):

array5[i][5]=array5[i][5].astype(np.float32)/100000      ## 没有缩进??

解决方法:

for i in range(len(data2)):                           

data2[i][5]=float(data2[i][5])/100000        ##  缩放成交量


两种方法 将字符串转换为 Pandas DataFrame 中的浮点数:

https://www.lsbin.com/7026.html

(1) astype(float)

df['DataFrame Column'] = df['DataFrame Column'].astype(float)

例如:

import pandas as pd

data = {'Product': ['ABC','XYZ'],

          'Price': ['250','270']

        }

df = pd.DataFrame(data)

df['Price'] = df['Price'].astype(float)

(2) to_numeric

df['DataFrame Column'] = pd.to_numeric(df['DataFrame Column'],errors='coerce')

例如:

import pandas as pd

data = {'Product': ['AAA','BBB','CCC','DDD'],

          'Price': ['250','ABC260','270','280XYZ']

        }

df = pd.DataFrame(data)

df['Price'] = pd.to_numeric(df['Price'], errors='coerce')



debug       TypeError: 'tuple' object is not callable      , tuple对象不能被调用 !


a.shape是一个turple数据类型,你在后面加“()”,相当于把a.shape看成了一个函数名,

a.shape(), 报错原因:,相当于调用a.shape函数

a.shape     正确格式

a.shape[ ]   正确格式

报错原因

import numpy as np

a = np.zeros([5,5])

print(a)

print(type(a))

print(a.shape)      《《《《《《《《《《《《《《《《《《《

print(type(a.shape))

#  输出:

[[0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0.]]

<class 'numpy.ndarray'>

(5, 5)                   《《《《《《《《《《《《《《《《《《《

<class 'tuple'>

a.shape是一个turple数据类型,你在后面加“()”,相当于把a.shape看成了一个函数名,a.shape(),相当于调用a.shape函数,因此会报错:tuple对象不能被调用 !

所以下次再出现这种问题,首先先看看“[ ]” 和“()”是否用错了,是否误把数据类型当做函数调用。


debug   TypeError: ‘int‘ object is not iterable


原因是循环中使用的应该是一组数,将

for i in len(A)

改成

for i in range(len(A))

即可


debug   AttributeError: 'int' object has no attribute 'astype'

原来是

x_data = x_data.astype('float32') / 255.0

解决问题使用如下处理方式

x_data = np.array(x_data,dtype=np.float32)/ 255.0



debug    AttributeError: 'int' object has no attribute 'astype'

原因是python数据和numpy数据类型的问题。

HangQingShrink2 =  HangQingSplit2 

for i in range(len(HangQing12)):

    HangQingShrink2[i][5]=HangQingShrink2[i][5].astype(np.float32)/100000  

#改成

HangQingShrink2 = np.array (HangQingSplit2)

for i in range(len(HangQing12)):

    HangQingShrink2[i][5]=HangQingShrink2[i][5].astype(np.float32)/100000



debug 在jupyter notebook 中的程序输出和cell输出


print(p.shape) 

##  作为程序输出      (3, 4)

p.shape        

##  作为单元格输出    output[xx](3, 4)


debug  IndentationError: unexpected indent


##  缩进错误



debug ValueError: invalid literal for int() with base 10: '

https://www.pythonforbeginners.com/exceptions/valueerror-invalid-literal-for-int-with-base-10#:~:text=1.Python%20%EE%80%80ValueError%3A%20invalid%20literal%20for%20int%20%28%29%20with,the%20%EE%80%80int%EE%80%81%20%28%29%20function%20due%20to%20which%20

Python ValueError: invalid literal for int() with base 10 当 int() 函数的输入包含非0-9的其他字符,包括空格等字符,因此输入无法转换为整数时,会报错


相关文章

  • 20220105 Python Debug

    debug np.array 不支持每个元素字符串使用 + 直接拼合 ---> 60 stockListName2...

  • PyCharm Python3 Debug环境搭建

    切换默认配置Python2.7到Python3.5后,debug功能异常,依次解决。 1,执行debug,报错:w...

  • python 获取debug模式

    python获取debug模式,给debug运行添加参数,经过测试,可以通过下面代码来判断当前是否debug运行:...

  • django日志器

    首先了解一下python的logging模块 import logginglogging.debug('debug...

  • DeBug Python代码全靠print函数?换用这个一天2K

    print 函数已老,DeBug 该靠 PySnooper 了~ 小伙伴们,你们都怎样 DeBug Python ...

  • Python Debug

    1. 异常: 1.1 抛出异常 使用raise关键字抛出异常 1.2 捕获异常 2. 断言 禁用断言python ...

  • Python Debug

    常用方法 try...except 方法捕获异常 Python----面向对象---try....except的详...

  • Python debug

    Python debug 如何调试 python 程序? print 这个方法不说了 pdb ipdb requi...

  • python debug

  • Debug心得分享

    目录 一个知乎问题? 为什么学会Debug 很重要? 如何debug-python为例? 再论debug的重要性 ...

网友评论

      本文标题:20220105 Python Debug

      本文链接:https://www.haomeiwen.com/subject/zzbhcrtx.html