pandas :增加日期时间 [英] Pandas: increment datetime
本文介绍了 pandas :增加日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要对df列中的date
进行一些操作
I need to do some actions with date
in df column
buys['date_min'] = (buys['date'] - MonthDelta(1))
buys['date_min'] = (buys['date'] + timedelta(days=5))
但它返回
TypeError:日期时间/时间增量操作的不兼容类型[object]
TypeError: incompatible type [object] for a datetime/timedelta operation
如何将其列?
推荐答案
I think you need first convert column date
to_datetime
, because type
od values in column date
is string
:
buys['date_min'] = (pd.to_datetime(buys['date']) - MonthDelta(1))
buys['date_min'] = (pd.to_datetime(buys['date']) + timedelta(days=5))
您需要参数 format
到 to_timedelta
You need parameter format
to to_datetime
and then another solution is with to_timedelta
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
这篇关于 pandas :增加日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文