我需要采取一些行动date
在 df 列中
buys['date_min'] = (buys['date'] - MonthDelta(1))
buys['date_min'] = (buys['date'] + timedelta(days=5))
但它返回
类型错误:日期时间/时间增量操作的类型 [对象] 不兼容
我怎样才能做到这一点?
我认为你需要先转换列date
to_datetime http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html, 因为type
列中的 od 值date
is string
:
buys['date_min'] = (pd.to_datetime(buys['date']) - MonthDelta(1))
buys['date_min'] = (pd.to_datetime(buys['date']) + timedelta(days=5))
EDIT:
你需要参数format http://strftime.org/ to to_datetime http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html然后另一个解决方案是to_timedelta http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_timedelta.html
buys = pd.DataFrame({'date':['01.01.2016','20.02.2016']})
print (buys)
date
0 01.01.2016
1 20.02.2016
buys['date']= pd.to_datetime(buys['date'],format='%d.%m.%Y')
buys['date_min'] = buys['date'] + pd.to_timedelta(5,unit='d')
print (buys)
date date_min
0 2016-01-01 2016-01-06
1 2016-02-20 2016-02-25
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)