问题描述
来自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 = ','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 =的工作原理。示例代码执行正确的操作,只是不是以所需的方式。帮助将不胜感激。
使用 date_parser
参数。通常,默认为 dateutil.parser.parse
,但您可以指定一个自定义函数(如 addtime
),它不仅解析日期字符串,而且还添加 timedelta
。
从StringIO import StringIO
导入datetime为dt
导入pandas为pd
导入dateutil.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 = ','field1','field'],index_col = None,
parse_dates = ['date'],header = None,date_parser = addtime)
print tdata
产生
field1字段
0 2012-12-31 16:00:00 5100 5200
1 2013-01-01 16:00:00 5300 5400
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
I would like to add 16 hours to each date as part of the Pandas CSV reader. but I can't figure out how to add a "parse_dates=" that works. The sample code performs the operation correctly, just not in the desired manner. Help would be appreciated.
解决方案Use the
date_parser
parameter. Ordinarily, that defaults todateutil.parser.parse
, but you can specify a custom function (such asaddtime
, below) which not only parses the date string but also adds atimedelta
as well.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
yields
date field1 field 0 2012-12-31 16:00:00 5100 5200 1 2013-01-01 16:00:00 5300 5400
这篇关于在pandas csv阅读器中的日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!