在Python和Pandas中使用dd.mm.yyyy读取csv [英] Read csv with dd.mm.yyyy in Python and Pandas

查看:118
本文介绍了在Python和Pandas中使用dd.mm.yyyy读取csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取德语日期格式的csv文件. 在这篇文章中似乎工作正常:

I am reading a csv file with German date format. Seems like it worked ok in this post:

使用pandas/python从导入的CSV中选择日期

但是,在我看来,日期似乎不被识别. 我在测试文件中找不到任何错误的字符串.

However, it seems like in my case the date is not recognized as such. I could not find any wrong string in the test file.

import pandas as pd
import numpy as np


%matplotlib inline
import matplotlib.pyplot as plt

from matplotlib import style
from pandas import DataFrame

style.use('ggplot')

df = pd.read_csv('testdata.csv', dayfirst=True, parse_dates=True)
df[:5]

结果是:

因此,带有日期的列无法这样识别. 我在这里做错了什么? 还是这种日期格式根本不兼容?

So, the Column with the dates is not recognized as such. What am I doing wrong here? Or is this date format simply not compatible?

  • OSX 10.10.3
  • Anaconda conda 3.13.0
  • Python 3.4.3-0
  • iPython笔记本3.1.0

推荐答案

如果使用parse_dates=True,则read_csv尝试

If you use parse_dates=True then read_csv tries to parse the index as a date. Therefore, you would also need to declare the first column as the index with index_col=[0]:

In [216]: pd.read_csv('testdata.csv', dayfirst=True, parse_dates=True, index_col=[0])
Out[216]: 
            morgens  mittags  abends
Datum                               
2015-03-16      382      452     202
2015-03-17      288      467     192

或者,如果您不希望Datum列成为索引,则可以使用 parse_dates=[0]明确告诉read_csv将第一列解析为日期:

Alternatively, if you don't want the Datum column to be an index, you could use parse_dates=[0] to explicitly tell read_csv to parse the first column as dates:

In [217]: pd.read_csv('testdata.csv', dayfirst=True, parse_dates=[0])
Out[217]: 
       Datum  morgens  mittags  abends
0 2015-03-16      382      452     202
1 2015-03-17      288      467     192


内幕下read_csv使用dateutil.parser.parse解析日期字符串:


Under the hood read_csv uses dateutil.parser.parse to parse date strings:

In [218]: import dateutil.parser as DP

In [221]: DP.parse('16.03.2015', dayfirst=True)
Out[221]: datetime.datetime(2015, 3, 16, 0, 0)

由于dateutil.parser可以轻松解析DD.MM.YYYY格式的日期字符串,因此您不必在此处声明自定义日期解析器.

Since dateutil.parser has no trouble parsing date strings in DD.MM.YYYY format, you don't have to declare a custom date parser here.

这篇关于在Python和Pandas中使用dd.mm.yyyy读取csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆