簡體   English   中英

如何將導入python的數據從csv文件轉換為時間序列?

[英]How can I turn data imported into python from a csv file to time-series?

我想通過.csv文件將導入python的數據轉換為時間序列。

GDP = pd.read_csv('GDP.csv')

[87]: GDP
   Out[87]: 
  GDP growth (%)
0              0.5
1             -5.2
2             -7.9
3             -9.1
4            -10.3
5             -8.8
6             -7.4
7            -10.1
8             -8.4
9             -8.7
10            -7.9
11            -4.1

由於通過.csv文件導入的數據是DataFrame格式,我首先嘗試將它們轉換為pd.Series:

GDP2 = pd.Series(data = GDP, index = pd.date_range(start = '01-2010', end = '01-2018', freq = 'Q'))

但是我看起來像這樣:

GDP2
Out[90]: 
2010-03-31    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2010-06-30    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2010-09-30    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2010-12-31    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2011-03-31    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2011-06-30    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2011-09-30    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2011-12-31    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2012-03-31    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2012-06-30    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2012-09-30    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))
2012-12-31    (G, D, P,  , g, r, o, w, t, h,  , (, %, ))

當我嘗試通過pd.DataFrame執行此操作時也是如此:

GDP2 = pd.DataFrame(data = GDP, index = pd.date_range(start = '01-2010', end = '01-2018', freq = 'Q'))

GDP2
Out[92]: 
        GDP growth (%)
2010-03-31             NaN
2010-06-30             NaN
2010-09-30             NaN
2010-12-31             NaN
2011-03-31             NaN
2011-06-30             NaN
2011-09-30             NaN
2011-12-31             NaN
2012-03-31             NaN
2012-06-30             NaN
2012-09-30             NaN

或者當我通過使用reindex()嘗試這個時:

dates = pd.date_range(start = '01-2010', end = '01-2018', freq = 'Q')

dates
Out[100]: 
DatetimeIndex(['2010-03-31', '2010-06-30', '2010-09-30', '2010-12-31',
           '2011-03-31', '2011-06-30', '2011-09-30', '2011-12-31',
           '2012-03-31', '2012-06-30', '2012-09-30', '2012-12-31',
           '2013-03-31', '2013-06-30', '2013-09-30', '2013-12-31',
           '2014-03-31', '2014-06-30', '2014-09-30', '2014-12-31',
           '2015-03-31', '2015-06-30', '2015-09-30', '2015-12-31',
           '2016-03-31', '2016-06-30', '2016-09-30', '2016-12-31',
           '2017-03-31', '2017-06-30', '2017-09-30', '2017-12-31'],
          dtype='datetime64[ns]', freq='Q-DEC')

GDP.reindex(dates)

Out[101]: 
       GDP growth (%)
2010-03-31             NaN
2010-06-30             NaN
2010-09-30             NaN
2010-12-31             NaN
2011-03-31             NaN
2011-06-30             NaN
2011-09-30             NaN
2011-12-31             NaN
2012-03-31             NaN
2012-06-30             NaN
2012-09-30             NaN
2012-12-31             NaN

我肯定會犯一些愚蠢的,新手的錯誤,如果有人能幫到我,我真的很感激。 干杯。

使用set_index

df
    gdp
0   0.5
1   -5.2
2   -7.9
3   -9.1
4   -10.3
5   -8.8
6   -7.4
7   -10.1
8   -8.4
9   -8.7
10  -7.9
11  -4.1

df = df.set_index(pd.date_range(start = '01-2010', end = '01-2013',freq = 'Q'))

            gdp
2010-03-31  0.5
2010-06-30  -5.2
2010-09-30  -7.9
2010-12-31  -9.1
2011-03-31  -10.3
2011-06-30  -8.8
2011-09-30  -7.4
2011-12-31  -10.1
2012-03-31  -8.4
2012-06-30  -8.7
2012-09-30  -7.9
2012-12-31  -4.1

修復代碼添加values

GDP2 = pd.DataFrame(data = GDP.values, index = pd.date_range(start = '01-2010', end = '01-2013',freq = 'Q'))
GDP2
Out[71]: 
               0
2010-03-31   0.5
2010-06-30  -5.2
2010-09-30  -7.9
2010-12-31  -9.1
2011-03-31 -10.3
2011-06-30  -8.8
2011-09-30  -7.4
2011-12-31 -10.1
2012-03-31  -8.4
2012-06-30  -8.7
2012-09-30  -7.9
2012-12-31  -4.1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM