我有一个历史悠久的代码段,这次似乎随机中断了。该错误是类型比较错误:
df = df[df['loaded'] > last_load]
Traceback (most recent call last):
File "<ipython-input-13-62572f9caa45>", line 1, in <module>
df = df[df['loaded'] > last_mart]
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py", line 1201, in wrapper
pd.DatetimeIndex)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py", line 1098, in dispatch_to_index_op
result = op(left_idx, right)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\datetimes.py", line 133, in wrapper
(type(other), str(other)))
TypeError: <class 'datetime.date'> type object 2018-08-15
代码段非常简单:
df['loaded'] = pd.to_datetime(df['loaded'])
last_load = datetime.datetime.now().date() - datetime.timedelta(weeks = 2)
df = df[df['loaded'] >= last_load]
在查看类型时,我已经确认last_load为datetime.date,并且df ['loaded']为datetime64 [ns]。
查看数据帧中的直接数据,格式上没有随机变化或奇数新的数据点。该错误似乎是在引用last_load,因为那是last_load定义所指定的日期。
我不确定是什么问题。有帮助吗?
答案 0 :(得分:0)
自上次运行此脚本以来,我已经更新了熊猫,由于某种原因,熊猫在分析时不再将日期时间自动转换为str。因此,简单的日期时间str包装器可以解决此问题。这是针对python 3.6的。
last_mart = datetime.datetime.now().date() - datetime.timedelta(weeks = 2)
df = df[df['loaded'] >= str(last_mart)]
df = df.drop_duplicates(subset = 'sdc_id')