简体   繁体   English

在python中不同的datetype值之间减去

[英]Subtracting between different datetype values in python

I have a DataFrame that looks like this: 我有一个看起来像这样的DataFrame:

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)

I get the following error: 我收到以下错误:

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 您可以在此处阅读类似的答案: pandas date列减法

Would have commented instead of an answer, but I do not have enough rep to do so. 本来可以发表评论而不是回答,但是我没有足够的代表这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM