简体   繁体   English

Python Pandas数据格式

[英]Python Pandas Data Formatting

I am in some sort of Python Pandas datetime purgatory and cannot seem to figure out why the below throws an error. 我在某种形式的Python Pandas日期时间炼狱中,似乎无法弄清楚以下内容引发错误的原因。 I have a simple date, a clear format string, and a thus far unexplained ValueError. 我有一个简单的日期,一个清晰的格式字符串,以及迄今为止无法解释的ValueError。 I've done quite a bit of searching, and can't seem to get to the bottom of this. 我已经做了大量的搜索工作,似乎无法深入了解这一点。

On top of the issue below, what is the concept surrounding the format string referred to as? 在下面的问题中,围绕格式字符串的概念是什么? In other words, where can I learn more about how the %m, %d, and %Y can be changed and reconfigured to specify different formats? 换句话说,在哪里可以了解有关如何更改和重新配置%m,%d和%Y以指定不同格式的信息?

Thanking you in advance from purgatory. 预先感谢炼狱。

In [19]: import pandas as pd

In [20]: date = '05-01-11'

In [21]: print type(date)
<type 'str'>

In [22]: pd.to_datetime(date, format = '%m-%d-%Y')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-24aff1dbfb25> in <module>()
----> 1 pd.to_datetime(date, format = '%m-%d-%Y')

/Users/amormachine/anaconda/lib/python2.7/site-packages/pandas/tseries/tools.pyc in to_datetime(arg, errors, dayfirst, utc, box, format, coerce, unit, infer_datetime_format)
    323         return _convert_listlike(arg, box, format)
    324 
--> 325     return _convert_listlike(np.array([ arg ]), box, format)[0]
    326 
    327 class DateParseError(ValueError):

/Users/amormachine/anaconda/lib/python2.7/site-packages/pandas/tseries/tools.pyc in _convert_listlike(arg, box, format)
    311                 return DatetimeIndex._simple_new(values, None, tz=tz)
    312             except (ValueError, TypeError):
--> 313                 raise e
    314 
    315     if arg is None:

ValueError: time data '05-01-11' does not match format '%m-%d-%Y'  --> THE HELL IT DOESN'T!

%Y is a four-digit year . %Y四位数的年份 You should find that %y will work. 您应该发现%y将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM