【问题标题】:How can I turn data imported into python from a csv file to time-series?如何将导入 python 的数据从 csv 文件转换为时间序列?
【发布时间】:2018-10-04 02:58:45
【问题描述】:

我想将通过 .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

我肯定犯了一些愚蠢的新手错误,如果有人能在这里帮助我,我将不胜感激。干杯。

【问题讨论】:

    标签: python pandas csv time-series date-range


    【解决方案1】:

    使用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
    

    【讨论】:

    • 非常感谢您花时间回答,这绝对有效!干杯!
    【解决方案2】:

    要修复您的代码,请添加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
    

    【讨论】:

    • 感谢您的大开眼界。我尝试对您的评论进行投票,但我显然缺乏这样做的声誉。干杯!
    • @Nick555 你应该按照你的想法,重建数据框是有效的
    • 我一定是误按了检查按钮。抱歉,伙计。
    • @Nick555 不用担心,编码愉快:-)
    • 干杯队友:-)
    猜你喜欢
    • 2019-10-11
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2019-07-16
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多