from StringIO import StringIO
import datetime as dt
import pandas as pd
def addtime(temp):
temp = (temp) + (dt.timedelta(hours =16))
return(temp)
data = """\
'12-31-2012',5100, 5200
'01/1/2013',5300,5400"""
tdata = pd.read_csv(StringIO(data),
names = ['date', 'field1', 'field'], index_col = None,
parse_dates =['date'], header= None)
print tdata
old_date = tdata.ix[0,'date']
print 'old date =',old_date
new_date = addtime(old_date)
print 'new date =',new_date
# date field1 field
#0 2012-12-31 00:00:00 5100 5200
#1 2013-01-01 00:00:00 5300 5400
#old date = 2012-12-31 00:00:00
#new date = 2012-12-31 16:00:00
我想在每个日期添加16个小时作为Pandas CSV阅读器的一部分。但我无法弄清楚如何添加一个有效的“parse_dates =”。示例代码正确执行操作,而不是以所需方式执行。帮助将不胜感激。
答案 0 :(得分:4)
使用date_parser
参数。通常,默认为dateutil.parser.parse
,但您可以指定自定义函数(例如下面的addtime
),它不仅可以解析日期字符串,还可以添加timedelta
。
from StringIO import StringIO
import datetime as dt
import pandas as pd
import dateutil.parser as parser
def addtime(temp):
temp = parser.parse(temp) + dt.timedelta(hours=16)
return temp
data = """\
'12-31-2012',5100, 5200
'01/1/2013',5300,5400"""
tdata = pd.read_csv(StringIO(data),
names = ['date', 'field1', 'field'], index_col = None,
parse_dates =['date'], header= None, date_parser=addtime)
print tdata
产量
date field1 field
0 2012-12-31 16:00:00 5100 5200
1 2013-01-01 16:00:00 5300 5400