使用pandas.to_datetime转换时指定日期格式 [英] Specifying date format when converting with pandas.to_datetime
问题描述
%d /%m /%Y
- 意思是: p> 12/01/2012
30/01/2012
上述示例代表2012年1月12日和2012年1月30日。
当我使用熊猫版本导入这些数据0.11.0我应用了以下转换:
将大熊猫导入为pd
...
cpts。 Date = cpts.Date.apply(pd.to_datetime)
但它不一致地转换日期。为了使用我现有的示例,12/01/2012将转换为2012年12月1日的datetime对象,但2012年1月30日,2012年1月30日将转换为2012年1月30日,这是我想要的。
在查看此问题后,我尝试了: p>
cpts.Date = cpts.Date.apply(pd.to_datetime,format ='%d /%m /%Y')
但结果是完全一样的。 源代码表明我正在做正确的事情,所以我是不知所措。有没有人知道我在做错什么?
你可以使用 parse_dates
read_csv 在读取数据时直接进行转换。
这里的诀窍是使用 dayfirst = True
表示您的日期从一天开始,而不是与当月开始。有关详细信息,请参阅: http://pandas.pydata.org/ pandas-docs / dev / generated / pandas.io.parsers.read_csv.html
当您的日期必须是索引时:
>>>将大熊猫导入为pd
>>> StringIO import StringIO
>>>> s = StringIO(date,value
... 12/01 / 2012,1
... 12/01 / 2012,2
... 30/01/2012 ,3)
>>>
>>> pd.read_csv(s,index_col = 0,parse_dates = True,dayfirst = True)
值
日期
2012-01-12 1
2012-01-12 2
2012-01-30 3
或当您的日期位于某一列时: p>
>>> s = StringIO(date
... 12/01/2012
... 12/01/2012
... 30/01/2012)
>>>>
>>> pd.read_csv(s,parse_dates = [0],dayfirst = True)
日期
0 2012-01-12 00:00:00
1 2012-01-12 00:00: 00
2 2012-01-30 00:00:00
I have data in a csv file with dates stored as strings in a standard UK format - %d/%m/%Y
- meaning they look like:
12/01/2012
30/01/2012
The examples above represent 12 January 2012 and 30 January 2012.
When I import this data with pandas version 0.11.0 I applied the following transformation:
import pandas as pd
...
cpts.Date = cpts.Date.apply(pd.to_datetime)
but it converted dates inconsistently. To use my existing example, 12/01/2012 would convert as a datetime object representing 1 December 2012 but 30/01/2012 converts as 30 January 2012, which is what I want.
After looking at this question I tried:
cpts.Date = cpts.Date.apply(pd.to_datetime, format='%d/%m/%Y')
but the results are exactly the same. The source code suggests I'm doing things right so I'm at a loss. Does anyone know what I'm doing wrong?
You can use the parse_dates
option from read_csv
to do the conversion directly while reading you data.
The trick here is to use dayfirst=True
to indicate your dates start with the day and not with the month. See here for more information: http://pandas.pydata.org/pandas-docs/dev/generated/pandas.io.parsers.read_csv.html
When your dates have to be the index:
>>> import pandas as pd
>>> from StringIO import StringIO
>>> s = StringIO("""date,value
... 12/01/2012,1
... 12/01/2012,2
... 30/01/2012,3""")
>>>
>>> pd.read_csv(s, index_col=0, parse_dates=True, dayfirst=True)
value
date
2012-01-12 1
2012-01-12 2
2012-01-30 3
Or when your dates are just in a certain column:
>>> s = StringIO("""date
... 12/01/2012
... 12/01/2012
... 30/01/2012""")
>>>
>>> pd.read_csv(s, parse_dates=[0], dayfirst=True)
date
0 2012-01-12 00:00:00
1 2012-01-12 00:00:00
2 2012-01-30 00:00:00
这篇关于使用pandas.to_datetime转换时指定日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!