python中不同日期类型值之间的减法 [英] Subtracting between different datetype values in python

查看:58
本文介绍了python中不同日期类型值之间的减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的 DataFrame:

I have a DataFrame that looks like this:

raw_data = {'Series_Date':['2017-03-10','2017-03-13','2017-03-14','2017-03-15'],'SeriesDate':['2017-03-10','2017-03-13','2017-03-14','2017-03-15']}
import pandas as pd
df = pd.DataFrame(raw_data,columns=['Series_Date','SeriesDate'])
print df

但是,在运行以下命令时:

However, on running the following comands:

from pandas.tseries.offsets import BDay
df['SeriesDate'] = pd.to_datetime(df['SeriesDate'])
df['Start_Date'] = df['SeriesDate'] - BDay(10)

我收到以下错误:

TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')

我该如何解决这个问题?

How can I work around this?

推荐答案

代码对我来说很好用.所以我猜你的环境存在一些问题.您可以在这里阅读类似的答案:熊猫日期列减法

Code works fine for me. So I'm guessing there's some problems in your environment. You can read up a similar answer here: pandas date column subtraction

本可以评论而不是回答,但我没有足够的代表来这样做.

Would have commented instead of an answer, but I do not have enough rep to do so.

这篇关于python中不同日期类型值之间的减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆