Python+pandas,判断日期进行drop的问题。
环境是Python+pandas,以下代码,是先对['派单日期'] 进行时间格式,再多条件drop,删除2020年之前的。['派单日期']的格式是:2020-12-12 10:23:22
import os
import numpy as np
import pandas as pd
from openpyxl import Workbook
# 格式化日期
sheetOnTheWay['派单日期'] = pd.to_datetime(sheetOnTheWay['派单日期'])
# 清洗不需要的行
sheetOnTheWay = sheetOnTheWay.drop(sheetOnTheWay.str.contains('测试') & sheetOnTheWay['派单日期'].year > 2020].index)
但以上代码报错,提示列没有属性year,这个怎么改?谢谢。
Traceback (most recent call last):
File "D:/python/pyJieDuanTongBao/pyJieDuanTongBao.py", line 118, in <module>
sheetOnTheWay = sheetOnTheWay.drop(sheetOnTheWay.str.contains('测试') & sheetOnTheWay['派单日期'].year != 2020].index)
File "D:\Program Files\anaconda\envs\playData\lib\site-packages\pandas\core\generic.py", line 5139, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'year' 本帖最后由 xixuedafa123 于 2020-12-8 12:50 编辑
https://blog.csdn.net/weixin_44216391/article/details/90107153
https://blog.csdn.net/xinxiangbobby/article/details/107716208?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control 已解决。
# 备注包含测试且派单日期在2020年前
sheetOnTheWay = sheetOnTheWay.drop(
sheetOnTheWay[(sheetOnTheWay['备注'].str.contains('测试')) & (sheetOnTheWay['派单日期'].dt.year < 2020)].index) {:301_1009:} 我一般把时间那列都设置成index了,调用的时候倒是不需要用.dt……啥时候测试看看
页:
[1]