在python pandas中处理欧洲日期格式

时间:2012-08-08 18:49:06

标签: python numpy pandas

这个问题在某种程度上是this one的延续。我已经能够正确地获取我对可下载的csv文件感兴趣的内容,如下所示

import time
import urllib2
import csv
import sys
import pandas
response=urllib2.urlopen('http://www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/hist_EURIBOR_2012.csv')
localFile = open('file.csv', 'w')
localFile.write(response.read())
localFile.close()
df2=pandas.io.parsers.read_csv('file.csv',index_col = 0, parse_dates = True, dayfirst = True)[:15].transpose()[:200] ## transpose in order to be compatible with pandas dataframe
df2 = df2.dropna() ## drop the values which are not-a-number
eur3m = df2['3m']

现在eur3m在Pandas中是Series,我希望获得有关给定时间段的信息。我知道我可以使用DateRange生成日期范围。我基本上想要做的是例如超过1个月的静态(假设2012年7月1日和2012年7月31日期间的平均值和标准值)。出于某些原因,虽然我读了csv文件试图解析日期,考虑到这些日期是欧洲格式(DD / MM / YYYY),我无法关注this example。让我们说尝试像

这样的东西
day=eur3m.index
i = ((day >= '01/07/2012') & (day <= '31/07/2012')) 

但它不起作用。实际上day是一个字符串数组。我不明白这是否正确。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

日期最初是作为列名读入的,而pandas目前不会将列名解析为日期。对于功能请求,请在github上创建一个新问题:https://github.com/pydata/pandas/issues

现在你可以进行一些后期处理:

eur3m.index = [datetime.datetime.strptime(x, '%d/%m/%Y') for x in eur3m.index]