skyhouse 发表于 2022-10-27 18:31:31

出现AttributeError错误,麻烦大家帮忙解决

打算读取csv数据后,把时间竖列的数值去除最后2位,并代替整列,但运行的时候,出现AttributeError错误,麻烦大家帮忙解决

文件无法上传,发个网盘地址
链接:pan.baidu.com/s/1JmNnddG0UpRJomDca9AX1A?pwd=n9w1
提取码:n9w1

时间
130548490
130548500
130548510
130548510
130548510
130548510
130548510
130548510
130548510
130548510
130548510
130548510
130548510
130548510
130548520
130548520
130548530
130548530
130548610
130551150
130551440
130551500
130551520
130551550
130551580
130552330
130552330



import pandas as pd
import numpy as np
ceshi = pd.read_csv("g:\测试.csv",encoding='gb2312')
ceshi['时间'] = ceshi.时间.str[:-2]
print(ceshi)

Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/pythonProject/venv/Scripts/2.py", line 4, in <module>
    ceshi['时间'] = ceshi.时间.str[:-2]
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\generic.py", line 5902, in __getattr__
    return object.__getattribute__(self, name)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\accessor.py", line 182, in __get__
    accessor_obj = self._accessor(obj)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\strings\accessor.py", line 181, in __init__
    self._inferred_dtype = self._validate(data)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\strings\accessor.py", line 235, in _validate
    raise AttributeError("Can only use .str accessor with string values!")
AttributeError: Can only use .str accessor with string values!

skyhouse 发表于 2022-10-27 18:32:43

没权限发布csv文件,想共享网盘网址也不行。。。。哎

hrpzcf 发表于 2022-10-27 18:36:26

报错:只能对字符串使用.str访问器,说明ceshi.时间    返回的不是字符串
页: [1]
查看完整版本: 出现AttributeError错误,麻烦大家帮忙解决