绘制图表,其x标签是月份和日期

时间:2017-03-29 06:39:08

标签: python pandas matplotlib

我有一个像这样的数据框

    2015max  2015min  idxmax  idxmin
01-05    242.0    -54.0   241.0  -127.0
01-26    245.0    -45.0   238.0  -134.0
04-02    298.0    -23.0   280.0   -59.0
04-04    288.0     72.0   283.0   -86.0
04-17    281.0     29.0   278.0   -47.0

我希望覆盖任何点的数据散布,其x-lable是一年如“01-01,01-02,01-03 ......” 我试过用

idxmin.index = pd.to_datetime(idxmin.index, format='%m-%d',errors='ignore') 

但它总是让我想起错误:

  

ValueError:无法将字符串转换为float

有人有个好主意解决问题吗?

1 个答案:

答案 0 :(得分:3)

您的数据中似乎有一些错误值,因此需要参数error='coerce'将其替换为NaT,然后将NaT替换为某个值:

print (idxmin)
       2015max  2015min  idxmax  idxmin
01-05    242.0    -54.0   241.0  -127.0
01-26    245.0    -45.0   238.0  -134.0
04-02    298.0    -23.0   280.0   -59.0
04-04    288.0     72.0   283.0   -86.0
04-35    281.0     29.0   278.0   -47.0 <- change last value to bad for testing

idxmin.index = pd.to_datetime(idxmin.index, format='%m-%d',errors='coerce') 
print (idxmin)
            2015max  2015min  idxmax  idxmin
1900-01-05    242.0    -54.0   241.0  -127.0
1900-01-26    245.0    -45.0   238.0  -134.0
1900-04-02    298.0    -23.0   280.0   -59.0
1900-04-04    288.0     72.0   283.0   -86.0
NaT           281.0     29.0   278.0   -47.0

idxmin.index = idxmin.index.fillna(pd.to_datetime('01-01-2000'))
print (idxmin)
            2015max  2015min  idxmax  idxmin
1900-01-05    242.0    -54.0   241.0  -127.0
1900-01-26    245.0    -45.0   238.0  -134.0
1900-04-02    298.0    -23.0   280.0   -59.0
1900-04-04    288.0     72.0   283.0   -86.0
2000-01-01    281.0     29.0   278.0   -47.0

您还可以检查所有错误值:

print (idxmin.index[pd.isnull(pd.to_datetime(idxmin.index, format='%m-%d',errors='coerce'))])
Index(['04-35'], dtype='object')