熊猫-将d-mmm-yy转换为datetime对象

时间:2018-10-31 20:23:26

标签: python pandas

我有一个CSV,其中包含一些类似如下的数据:

excel doc

我有很多这样的文件,我想将它们读入DataFrame:

     Warning in grep("\n", x) :
     Warning in grep("^(---|\\.\\.\\.)\\s*$", lines) :
     processing file: reporte.Rmd
       string de entrada 43 es inválida en este locale
       string de entrada 43 es inválida en este locale
       string de entrada 43 es inválida en este locale

       string de entrada 43 es inválida en este locale

     Warning in grepl(chunk.end, lines) :

      |                                                                       
      |                                                                 |   0%
      |                                                                       
      |...........                                                      |  17%
     Warning in grepl(chunk.begin, lines) :

       ordinary text without R code
     List of 1

      |                                                                       
      |......................                                           |  33%
      $ echo: logi FALSE

     label: unnamed-chunk-1 (with options) 


      |                                                                       
      |...........................................                      |  67%

      |                                                                       
      |................................                                 |  50%
     label: unnamed-chunk-2 (with options) 
       ordinary text without R code
      $ echo   : logi FALSE
     List of 2
      $ results: chr "asis" 
      |                                                                       
      |......................................................           |  83%
        inline R code fragments


     Quitting from lines 39-46 (reporte.Rmd) 

     Warning: Error in nchar: invalid multibyte string, element 1
       [No stack trace available]

我尝试了多种格式,但是似乎都没有用。有其他选择吗?

3 个答案:

答案 0 :(得分:3)

实际上,您不需要在此处指定格式。格式为明确,如果我们在不指定格式的情况下进行转换,则会得到:

>>> df
       Date
0  1-Dec-99
1  1-Jul-99
2  1-Jun-99
3  1-Nov-99
4  1-Oct-99
5  1-Sep-99
6  2-Aug-99
7  2-Dec-99
>>> pd.to_datetime(df['Date'])
0   1999-12-01
1   1999-07-01
2   1999-06-01
3   1999-11-01
4   1999-10-01
5   1999-09-01
6   1999-08-02
7   1999-12-02
Name: Date, dtype: datetime64[ns]

或者,我们可以在datetime module [Python-doc]的文档中查找格式。我们在这里认为:

%d   Day of the month as a zero-padded       01, 02, …, 31
     decimal number.

%b   Month as locale’s abbreviated name.     Jan, Feb, …, Dec (en_US);
                                             Jan, Feb, …, Dez (de_DE)

%y   Year without century as a               00, 01, …, 99
     zero-padded decimal number.

因此我们可以将格式指定为:

>>> pd.to_datetime(df['Date'], format='%d-%b-%y')
0   1999-12-01
1   1999-07-01
2   1999-06-01
3   1999-11-01
4   1999-10-01
5   1999-09-01
6   1999-08-02
7   1999-12-02
Name: Date, dtype: datetime64[ns]

答案 1 :(得分:2)

使用%b连续三个月。请参阅Python strftime参考:http://strftime.org/

我认为您想要:w代表一天,b代表月份,yy代表年份。

我假设日期不是零填充的,如果日期是零填充的,请使用d而不是w

答案 2 :(得分:2)

查看datetimes here的指令。使用3个字母月和2位数字的年份,下面的方法应该起作用:

df['Fut Expiration Date'] = pd.to_datetime(df['Fut Expiration Date'], format='%d-%b-%y')