【问题标题】:Handle with European date format in python pandas在 python pandas 中处理欧洲日期格式
【发布时间】:2012-08-05 22:54:43
【问题描述】:

这个问题在某种程度上是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 日期间的平均值和标准)。由于某些原因,尽管考虑到这些日期是欧洲格式 (DD/MM/YYYY),我阅读了 csv 文件试图解析日期,但我无法关注this example。假设尝试类似

day=eur3m.index
i = ((day >= '01/07/2012') & (day <= '31/07/2012')) 

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

【问题讨论】:

    标签: python numpy pandas


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的提示:它解决了我的疑虑以及对日期和日期时间模块的一些操作
    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多