pd.read_如何使用parse_dates,然后将索引设置为解析日期的结果



是否有可能在一行调用中读取该文件:

http://www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_index/monthly.ao.index.b50.current.ascii

包含如下数据:

 1950    1  -0.60310E-01
 1950    2   0.62681E+00
 1950    3  -0.81275E-02
 1950    4   0.55510E+00
 1950    5   0.71577E-01
 1950    6   0.53857E+00
 1950    7  -0.80248E+00
 1950    8  -0.85101E+00
 1950    9   0.35797E+00
 1950   10  -0.37890E+00
 1950   11  -0.51511E+00
 1950   12  -0.19281E+01
 1951    1  -0.84969E-01
 1951    2  -0.39993E+00
 1951    3  -0.19341E+01
 1951    4  -0.77648E+00
 1951    5  -0.86278E+00
 1951    6  -0.91786E+00
 1951    7   0.90023E-01
 1951    8  -0.37741E+00
 1951    9  -0.81778E+00
 1951   10  -0.21291E+00

以一种方式,将返回一个pd.DataFrame与一个日期时间索引从应用parse_dates参数到最左边的两个列?

这是我的调用,产生一个DataFrame,但我希望index是日期时间列:

data = pd.read_fwf("http://www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_i`ndex/monthly.ao.index.b50.current.ascii", parse_dates = [[0, 1]], infer_datetime_format = True, header = None)

可以添加index_col = 0参数:

df = pd.read_fwf('http://www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_index/monthly.ao.index.b50.current.ascii',
                 parse_dates = [[0, 1]], 
                 infer_datetime_format = True, 
                 header = None, 
                 index_col = 0)

print (df)
                   2
0_1                 
1950-01-01 -0.060310
1950-02-01  0.626810
1950-03-01 -0.008128
1950-04-01  0.555100
1950-05-01  0.071577
...
...
print (df.index)
DatetimeIndex(['1950-01-01', '1950-02-01', '1950-03-01', '1950-04-01',
               '1950-05-01', '1950-06-01', '1950-07-01', '1950-08-01',
               '1950-09-01', '1950-10-01',
               ...
               '2015-08-01', '2015-09-01', '2015-10-01', '2015-11-01',
               '2015-12-01', '2016-01-01', '2016-02-01', '2016-03-01',
               '2016-04-01', '2016-05-01'],
              dtype='datetime64[ns]', name='0_1', length=797, freq=None)

相关内容

  • 没有找到相关文章

最新更新