简体   繁体   English

熊猫:增加日期时间

[英]Pandas: increment datetime

I need to do some actions with date in df column 我需要在df列中使用date执行一些操作

buys['date_min'] = (buys['date'] - MonthDelta(1))
buys['date_min'] = (buys['date'] + timedelta(days=5))

But it return 但它又回来了

TypeError: incompatible type [object] for a datetime/timedelta operation TypeError:日期时间/ timedelta操作的不兼容类型[object]

How can I do it to column? 我该怎么做到列?

I think you need first convert column date to_datetime , because type od values in column date is string : 我想你需要先转换柱date to_datetime ,因为type列OD值datestring

buys['date_min'] = (pd.to_datetime(buys['date']) - MonthDelta(1))
buys['date_min'] = (pd.to_datetime(buys['date']) + timedelta(days=5))

EDIT: 编辑:

You need parameter format to to_datetime and then another solution is with to_timedelta 你需要参数format to_datetime然后另一个解决方案是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

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

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