時(shí)間序列分析

時(shí)間序列簡(jiǎn)介

時(shí)間序列分析是數(shù)據(jù)分析過(guò)程中,尤其是在金融數(shù)據(jù)分析過(guò)程中會(huì)經(jīng)常遇到的。時(shí)間序列,就是以時(shí)間排序的一組隨機(jī)變量。例如國(guó)家統(tǒng)計(jì)局每年或每月定期發(fā)布的 GDP 或 CPI 指數(shù);24 小時(shí)內(nèi)某一股票、基金、指數(shù)的數(shù)值變化等,都是時(shí)間序列。

時(shí)間序列處理

當(dāng)拿到一些時(shí)間序列的原始數(shù)據(jù)時(shí),可能會(huì)遇到一些情況:
1.某一段時(shí)間缺失,需要填充。
2.時(shí)間序列錯(cuò)位,需要對(duì)齊。
3.數(shù)據(jù)表a和數(shù)據(jù)表b所采用的時(shí)間間隔不一致,需要重新采樣。
......
面對(duì)這些問(wèn)題,就需要通過(guò)一些手段來(lái)獲得最終想要的數(shù)據(jù)。

Timestamp時(shí)間戳

時(shí)間戳,即代表一個(gè)時(shí)間時(shí)刻??梢灾苯佑胮d.Timestamp()來(lái)創(chuàng)建時(shí)間戳。

In [1]: import pandas as pd

In [2]: pd.Timestamp("2018-11-1")
Out[2]: Timestamp('2018-11-01 00:00:00')

In [3]: pd.Timestamp(2018,11,1)
Out[3]: Timestamp('2018-11-01 00:00:00')

In [4]: pd.Timestamp("2018-11-1 11:31:00")
Out[4]: Timestamp('2018-11-01 11:31:00')

時(shí)間戳索引

單個(gè)時(shí)間戳為Timestamp數(shù)據(jù),當(dāng)時(shí)間戳以列表形式存在時(shí),Pandas將強(qiáng)制轉(zhuǎn)換為DatetimeIndex。這時(shí)就不能再使用pd.Timestamp()來(lái)創(chuàng)建時(shí)間戳了,而是pd.to_datetime()來(lái)創(chuàng)建:

In [5]: pd.to_datetime(["2018-11-1", "2018-11-2", "2018-11-3"])
Out[5]: DatetimeIndex(['2018-11-01', '2018-11-02', '2018-11-03'], dtype='datetime64[ns]', freq=None)

pd.to_datetime()不僅僅可用來(lái)創(chuàng)建DatetimeIndex,還可以將時(shí)間戳序列格式進(jìn)行轉(zhuǎn)換等操作。常見的時(shí)間戳?xí)鴮憳邮蕉伎梢酝ㄟ^(guò)pd.to_datetime()規(guī)范化。

In [7]: pd.to_datetime(['Jul 10, 2018', '2018-12-12', None])
Out[7]: DatetimeIndex(['2018-07-10', '2018-12-12', 'NaT'], dtype='datetime64[ns]', freq=None)

In [8]: pd.to_datetime(['2017/10/11', '2018/11/1'])
Out[8]: DatetimeIndex(['2017-10-11', '2018-11-01'], dtype='datetime64[ns]', freq=None)

對(duì)于歐洲時(shí)區(qū)普遍采用的書寫樣式,可以通過(guò)dayfirst=True參數(shù)進(jìn)行修正:

In [9]: pd.to_datetime('1-10-2018')
Out[9]: Timestamp('2018-01-10 00:00:00')

In [10]: pd.to_datetime('1-10-2018', dayfirst=True)
Out[10]: Timestamp('2018-10-01 00:00:00')

Pandas熟悉的Series和DataFrame格式的字符串,也可以直接通過(guò)to_datetime轉(zhuǎn)換:

In [11]: pd.to_datetime(pd.Series(['2018-11-1', '2018-11-2', '2018-11-3']))
Out[11]:
0   2018-11-01
1   2018-11-02
2   2018-11-03
dtype: datetime64[ns]

In [12]: pd.to_datetime(['2018-11-1', '2018-11-2', '2018-11-3'])
Out[12]: DatetimeIndex(['2018-11-01', '2018-11-02', '2018-11-03'], dtype='datetime64[ns]', freq=None)

In [13]: pd.to_datetime(pd.DataFrame({'year': [2017, 2017], 'month': [1, 2],
    ...:  'day': [3, 4], 'hour': [5, 6]}))
Out[13]:
0   2017-01-03 05:00:00
1   2017-02-04 06:00:00
dtype: datetime64[ns]

其中:

  • pd.to_datetime(Series/DataFrame)返回的是Series。
  • pd.to_datetime(List)返回的是DatetimeIndex。

如果要轉(zhuǎn)換如上所示的DataFrame,必須存在的列名有year,month,day。另外 hour, minute, second, millisecond, microsecond, nanosecond可選。

當(dāng)使用pd.to_datetime() 轉(zhuǎn)換數(shù)據(jù)時(shí),很容易遇到無(wú)效數(shù)據(jù)。有一些任務(wù)對(duì)無(wú)效數(shù)據(jù)非??量?,所以報(bào)錯(cuò)讓我們找到這些無(wú)效數(shù)據(jù)是不錯(cuò)的方法。當(dāng)然,也有一些任務(wù)不在乎零星的無(wú)效數(shù)據(jù),這時(shí)候就可以選擇忽略。

# 遇到無(wú)效數(shù)據(jù)報(bào)錯(cuò)
In [15]: pd.to_datetime(['2018-11-1', 'invalid'], errors='raise')
ValueError: ('Unknown string format:', 'invalid')

# 忽略無(wú)效數(shù)據(jù)
In [17]: pd.to_datetime(['2018-11-1', 'invalid'], errors='ignore')
Out[17]: array(['2018-11-1', 'invalid'], dtype=object)

# 將無(wú)效數(shù)據(jù)顯示為NaT
In [18]: pd.to_datetime(['2018-11-1', 'invalid'], errors='coerce')
Out[18]: DatetimeIndex(['2018-11-01', 'NaT'], dtype='datetime64[ns]', freq=None)

生成DatetimeIndex的另一個(gè)重要方法pandas.data_range。
可以通過(guò)指定一個(gè)規(guī)則,讓pandas.data_range生成有序的DatetimeIndex.
pandas.data_range帶有的默認(rèn)參數(shù)如下:

pandas.date_range(start=None, end=None, periods=None, freq=’D’, tz=None, normalize=False,
name=None, closed=None, **kwargs)

常用參數(shù)的含義如下:

  • start= : 設(shè)置起始時(shí)間
  • end= : 設(shè)置截至?xí)r間
  • periods= :設(shè)置時(shí)間區(qū)間,若None則需要單獨(dú)設(shè)置起止和截至?xí)r間。
  • freq= : 設(shè)置間隔周期。
  • 設(shè)置時(shí)區(qū)。

其中,freq= 參數(shù)是非常關(guān)鍵的參數(shù),可以設(shè)置的周期有:

  • freq='s' :秒
  • freq='min' :分鐘
  • freq='H' :小時(shí)
  • freq='D' :天
  • freq='w' :周
  • freq='m' :月
  • freq='BM' :每個(gè)月最后一個(gè)工作日
  • freq='W' : 每周的星期日
# 從2018-11-1 到 2018-11-2,以小時(shí)間隔
In [19]: pd.date_range('2018-11-1', '2018-11-2', freq='H')
Out[19]:
DatetimeIndex(['2018-11-01 00:00:00', '2018-11-01 01:00:00',
               '2018-11-01 02:00:00', '2018-11-01 03:00:00',
               '2018-11-01 04:00:00', '2018-11-01 05:00:00',
               '2018-11-01 06:00:00', '2018-11-01 07:00:00',
               '2018-11-01 08:00:00', '2018-11-01 09:00:00',
               '2018-11-01 10:00:00', '2018-11-01 11:00:00',
               '2018-11-01 12:00:00', '2018-11-01 13:00:00',
               '2018-11-01 14:00:00', '2018-11-01 15:00:00',
               '2018-11-01 16:00:00', '2018-11-01 17:00:00',
               '2018-11-01 18:00:00', '2018-11-01 19:00:00',
               '2018-11-01 20:00:00', '2018-11-01 21:00:00',
               '2018-11-01 22:00:00', '2018-11-01 23:00:00',
               '2018-11-02 00:00:00'],
              dtype='datetime64[ns]', freq='H')

# 從2018-11-1 開始,以1s為間隔,向后推 10 次
In [20]: pd.date_range('2018-11-1', periods=10, freq='s')
Out[20]:
DatetimeIndex(['2018-11-01 00:00:00', '2018-11-01 00:00:01',
               '2018-11-01 00:00:02', '2018-11-01 00:00:03',
               '2018-11-01 00:00:04', '2018-11-01 00:00:05',
               '2018-11-01 00:00:06', '2018-11-01 00:00:07',
               '2018-11-01 00:00:08', '2018-11-01 00:00:09'],
              dtype='datetime64[ns]', freq='S')

# 從2018-11-1 開始, 以 1H20min為間隔, 向后推 10 次
In [21]: pd.date_range('11/1/2018', periods=10, freq='1H20min')
Out[21]:
DatetimeIndex(['2018-11-01 00:00:00', '2018-11-01 01:20:00',
               '2018-11-01 02:40:00', '2018-11-01 04:00:00',
               '2018-11-01 05:20:00', '2018-11-01 06:40:00',
               '2018-11-01 08:00:00', '2018-11-01 09:20:00',
               '2018-11-01 10:40:00', '2018-11-01 12:00:00'],
              dtype='datetime64[ns]', freq='80T')

除了生成DatetimeIndex,還可以對(duì)已有的DatetimeIndex進(jìn)行操作。這些操作包括選擇,切片等。類似于對(duì)Series的操作。

In [23]: x = pd.date_range('2018-11-1', periods=10, freq='1D1H')

In [24]: x
Out[24]:
DatetimeIndex(['2018-11-01 00:00:00', '2018-11-02 01:00:00',
               '2018-11-03 02:00:00', '2018-11-04 03:00:00',
               '2018-11-05 04:00:00', '2018-11-06 05:00:00',
               '2018-11-07 06:00:00', '2018-11-08 07:00:00',
               '2018-11-09 08:00:00', '2018-11-10 09:00:00'],
              dtype='datetime64[ns]', freq='25H')

# 選取索引為1的時(shí)間戳
In [27]: x[1]
Out[27]: Timestamp('2018-11-02 01:00:00', freq='25H')

# 對(duì)索引從0到4的時(shí)間進(jìn)行切片
In [28]: x[:5]
Out[28]:
DatetimeIndex(['2018-11-01 00:00:00', '2018-11-02 01:00:00',
               '2018-11-03 02:00:00', '2018-11-04 03:00:00',
               '2018-11-05 04:00:00'],
              dtype='datetime64[ns]', freq='25H')

DateOffset對(duì)象

上面,使用freq='1D1H'參數(shù),可以生成間隔1天+1小時(shí)的時(shí)間戳索引。而在時(shí)間序列處理中,還常用到一種叫做DateOffset對(duì)象,可以對(duì)時(shí)間戳索引進(jìn)行更加靈活的變化。DateOffset對(duì)象主要作用有:

  1. 可以讓時(shí)間索引增加或減少一定時(shí)間段。
  2. 可以讓時(shí)間索引乘以一個(gè)整數(shù)。
  3. 可以讓時(shí)間索引向前或后移動(dòng)到下一個(gè)或上一個(gè)特定的偏移日期。
In [29]: from pandas import offsets

In [30]: a = pd.date_range('2018-11-1', periods=10, freq='1D1H')

In [31]: a
Out[31]:
DatetimeIndex(['2018-11-01 00:00:00', '2018-11-02 01:00:00',
               '2018-11-03 02:00:00', '2018-11-04 03:00:00',
               '2018-11-05 04:00:00', '2018-11-06 05:00:00',
               '2018-11-07 06:00:00', '2018-11-08 07:00:00',
               '2018-11-09 08:00:00', '2018-11-10 09:00:00'],
              dtype='datetime64[ns]', freq='25H')

# 使用DateOffset 對(duì)象讓a依次增加1個(gè)月+2天+3小時(shí)
In [33]: a + offsets.DateOffset(months=1, days=2, hours=3)
Out[33]:
DatetimeIndex(['2018-12-03 03:00:00', '2018-12-04 04:00:00',
               '2018-12-05 05:00:00', '2018-12-06 06:00:00',
               '2018-12-07 07:00:00', '2018-12-08 08:00:00',
               '2018-12-09 09:00:00', '2018-12-10 10:00:00',
               '2018-12-11 11:00:00', '2018-12-12 12:00:00'],
              dtype='datetime64[ns]', freq='25H')

# 使用DateOffset對(duì)象讓a向后偏移2周
In [34]: a + 2*offsets.Week()
Out[34]:
DatetimeIndex(['2018-11-15 00:00:00', '2018-11-16 01:00:00',
               '2018-11-17 02:00:00', '2018-11-18 03:00:00',
               '2018-11-19 04:00:00', '2018-11-20 05:00:00',
               '2018-11-21 06:00:00', '2018-11-22 07:00:00',
               '2018-11-23 08:00:00', '2018-11-24 09:00:00'],
              dtype='datetime64[ns]', freq='25H')

Period 時(shí)間間隔

Pandas 中還存在 Period 時(shí)間間隔和 PeriodIndex 時(shí)間間隔索引對(duì)象。它們用來(lái)定義一定時(shí)間跨度。

# 一年跨度
In [35]: pd.Period('2018')
Out[35]: Period('2018', 'A-DEC')

# 一個(gè)月跨度
In [36]: pd.Period('2018-11')
Out[36]: Period('2018-11', 'M')

# 一天跨度
In [37]: pd.Period('2018-11-1')
Out[37]: Period('2018-11-01', 'D')

# 一小時(shí)跨度
In [38]: pd.Period('2018-11-1 13')
Out[38]: Period('2018-11-01 13:00', 'H')

# 一分鐘跨度
In [39]: pd.Period('2018-11-1 13:22')
Out[39]: Period('2018-11-01 13:22', 'T')

# 一秒跨度
In [40]: pd.Period('2018-11-1 13:22:12')
Out[40]: Period('2018-11-01 13:22:12', 'S')

同樣可以通過(guò)pandas.period_range()方法來(lái)生成序列:

In [42]: pd.period_range('2017-11', '2018-11', freq='M')
Out[42]:
PeriodIndex(['2017-11', '2017-12', '2018-01', '2018-02', '2018-03', '2018-04',
             '2018-05', '2018-06', '2018-07', '2018-08', '2018-09', '2018-10',
             '2018-11'],
            dtype='period[M]', freq='M')

DatetimeIndex 的dtype 類型為 datetime64[ns],而 PeriodIndex 的 dtype 類型為 period[M]。另外,對(duì)于 Timestamp和 Period 的區(qū)別,在單獨(dú)拿出來(lái)看一下:

In [43]: pd.Period('2018-11-1')
Out[43]: Period('2018-11-01', 'D')

In [44]: pd.Timestamp('2018-11-1')
Out[44]: Timestamp('2018-11-01 00:00:00')

可以看到,上面代表是2017-01-01這一天,而下面僅代表 2017-01-01 00:00:00 這一時(shí)刻。

時(shí)序數(shù)據(jù)檢索

DatetimeIndex 之所以稱之為時(shí)間戳索引,當(dāng)然是它的主要用途是作為 Series 或者 DataFrame 的索引。下面,就隨機(jī)生成一些數(shù)據(jù),看一看如何對(duì)時(shí)間序列數(shù)據(jù)進(jìn)行操作。

In [1]: import numpy as np
In [2]: import pandas as pd

# 生成時(shí)間索引
In [3]: i = pd.date_range('2018-11-1', periods=20, freq='M')

# 生成隨機(jī)數(shù)據(jù)并添加時(shí)間作為索引
In [4]: data = pd.Series(np.random.randn(len(i)), index=i)

# 查看數(shù)據(jù)
In [5]: data
Out[5]:
2018-11-30   -1.040781
2018-12-31   -2.396724
2019-01-31    0.370134
2019-02-28   -1.655618
2019-03-31   -0.755367
2019-04-30   -1.465855
2019-05-31   -1.212847
2019-06-30   -0.816448
2019-07-31    0.360213
2019-08-31    0.100798
2019-09-30    1.004533
2019-10-31    0.488605
2019-11-30   -2.452875
2019-12-31   -1.495978
2020-01-31    0.535245
2020-02-29   -0.480371
2020-03-31   -0.536331
2020-04-30    0.640610
2020-05-31    0.271148
2020-06-30    0.522567
Freq: M, dtype: float64

上面就生成了一個(gè)以時(shí)間為索引的 Series 序列。這就回到了對(duì) Pandas 中 Series 和 DataFrame 類型數(shù)據(jù)操作的問(wèn)題。下面演示一些操作:

# 檢索2018年的所有數(shù)據(jù)
In [6]: data['2018']
Out[6]:
2018-11-30   -1.040781
2018-12-31   -2.396724
Freq: M, dtype: float64

# 檢索2019年7月到2020年3月之間的所有數(shù)據(jù)
In [7]: data['2019-07':'2020-03']
Out[7]:
2019-07-31    0.360213
2019-08-31    0.100798
2019-09-30    1.004533
2019-10-31    0.488605
2019-11-30   -2.452875
2019-12-31   -1.495978
2020-01-31    0.535245
2020-02-29   -0.480371
2020-03-31   -0.536331
Freq: M, dtype: float64

# 使用loc方法檢索2019年1月的所有數(shù)據(jù)
In [8]: data.loc['2019-01']
Out[8]:
2019-01-31    0.370134
Freq: M, dtype: float64

# 使用truncate方法檢索2019-3-1 到 2020-4-2 期間的數(shù)據(jù)
In [9]: data.truncate(before='2019-3-1', after='2020-4-2')
Out[9]:
2019-03-31   -0.755367
2019-04-30   -1.465855
2019-05-31   -1.212847
2019-06-30   -0.816448
2019-07-31    0.360213
2019-08-31    0.100798
2019-09-30    1.004533
2019-10-31    0.488605
2019-11-30   -2.452875
2019-12-31   -1.495978
2020-01-31    0.535245
2020-02-29   -0.480371
2020-03-31   -0.536331
Freq: M, dtype: float64

時(shí)間數(shù)據(jù)偏移

這里可能會(huì)用到 Shifting 方法,將時(shí)間索引進(jìn)行整體偏移。

# 生成時(shí)間索引
In [10]: i = pd.date_range('2017-1-1', periods=5, freq='M')

# 生成隨機(jī)數(shù)據(jù)并添加時(shí)間作為索引
In [11]: data = pd.Series(np.random.randn(len(i)), index=i)

# 查看數(shù)據(jù)
In [12]: data
Out[12]:
2017-01-31   -0.440074
2017-02-28    0.706395
2017-03-31    0.823844
2017-04-30    0.703313
2017-05-31    0.920151
Freq: M, dtype: float64

# 將索引向前移位3個(gè)單位,也就是數(shù)據(jù)向后位移3個(gè)單位,缺失數(shù)據(jù)Pandas會(huì)用NaN填充
In [13]: data.shift(3)
Out[13]:
2017-01-31         NaN
2017-02-28         NaN
2017-03-31         NaN
2017-04-30   -0.440074
2017-05-31    0.706395
Freq: M, dtype: float64

# 將索引向后位移3個(gè)單位,也就是數(shù)據(jù)向前位移3個(gè)單位
In [14]: data.shift(-3)
Out[14]:
2017-01-31    0.703313
2017-02-28    0.920151
2017-03-31         NaN
2017-04-30         NaN
2017-05-31         NaN
Freq: M, dtype: float64

# 將索引的時(shí)間向后移動(dòng)3天
In [15]: data.shift(3, freq='D')
Out[15]:
2017-02-03   -0.440074
2017-03-03    0.706395
2017-04-03    0.823844
2017-05-03    0.703313
2017-06-03    0.920151
dtype: float64

時(shí)間數(shù)據(jù)重采樣

除了 Shifting 方法,重采樣 Resample 也會(huì)經(jīng)常用到。Resample 可以提升或降低一個(gè)時(shí)間索引序列的頻率,大有用處。例如:當(dāng)時(shí)間序列數(shù)據(jù)量非常大時(shí),我們可以通過(guò)低頻率采樣的方法得到規(guī)模較小到時(shí)間覆蓋依然較為全面的新數(shù)據(jù)集。另外,對(duì)于多個(gè)不同頻率的數(shù)據(jù)集需要數(shù)據(jù)對(duì)齊時(shí),重采樣可以十分重要的手段。

In [16]: i = pd.date_range('2017-01-01', periods=20, freq='D')

In [17]: data = pd.Series(np.random.randn(len(i)), index=i)

In [18]: data
Out[18]:
2017-01-01    1.096656
2017-01-02   -2.404326
2017-01-03   -0.883177
2017-01-04    0.554299
2017-01-05   -1.004089
2017-01-06   -0.014365
2017-01-07   -0.514893
2017-01-08   -0.049173
2017-01-09    1.633568
2017-01-10    2.076252
2017-01-11    0.132104
2017-01-12   -1.011756
2017-01-13   -1.330824
2017-01-14    1.626463
2017-01-15   -0.339399
2017-01-16   -0.622435
2017-01-17   -0.201180
2017-01-18   -1.193216
2017-01-19   -1.522457
2017-01-20    1.217058
Freq: D, dtype: float64

# 按照2天進(jìn)行降采樣,并對(duì)2天對(duì)應(yīng)的數(shù)據(jù)求和作為新數(shù)據(jù)
In [19]: data.resample('2D').sum()
Out[19]:
2017-01-01   -1.307670
2017-01-03   -0.328878
2017-01-05   -1.018454
2017-01-07   -0.564066
2017-01-09    3.709820
2017-01-11   -0.879652
2017-01-13    0.295638
2017-01-15   -0.961834
2017-01-17   -1.394395
2017-01-19   -0.305399
dtype: float64

# 按照2天進(jìn)行降采樣,并對(duì)2天對(duì)應(yīng)的數(shù)據(jù)求平均值作為新數(shù)據(jù)
In [20]: data.resample('2D').mean()
Out[20]:
2017-01-01   -0.653835
2017-01-03   -0.164439
2017-01-05   -0.509227
2017-01-07   -0.282033
2017-01-09    1.854910
2017-01-11   -0.439826
2017-01-13    0.147819
2017-01-15   -0.480917
2017-01-17   -0.697198
2017-01-19   -0.152700
dtype: float64

# 按照2天進(jìn)行降采樣,并對(duì)2天對(duì)應(yīng)的數(shù)據(jù)求最大值作為新數(shù)據(jù)
In [21]: data.resample('2D').max()
Out[21]:
2017-01-01    1.096656
2017-01-03    0.554299
2017-01-05   -0.014365
2017-01-07   -0.049173
2017-01-09    2.076252
2017-01-11    0.132104
2017-01-13    1.626463
2017-01-15   -0.339399
2017-01-17   -0.201180
2017-01-19    1.217058
dtype: float64

# 按照2天進(jìn)行降采樣,并將2天對(duì)應(yīng)的數(shù)據(jù)的原值,最大值,最小值,以及臨近值列出
In [22]: data.resample('2D').ohlc()
Out[22]:
                open      high       low     close
2017-01-01  1.096656  1.096656 -2.404326 -2.404326
2017-01-03 -0.883177  0.554299 -0.883177  0.554299
2017-01-05 -1.004089 -0.014365 -1.004089 -0.014365
2017-01-07 -0.514893 -0.049173 -0.514893 -0.049173
2017-01-09  1.633568  2.076252  1.633568  2.076252
2017-01-11  0.132104  0.132104 -1.011756 -1.011756
2017-01-13 -1.330824  1.626463 -1.330824  1.626463
2017-01-15 -0.339399 -0.339399 -0.622435 -0.622435
2017-01-17 -0.201180 -0.201180 -1.193216 -1.193216
2017-01-19 -1.522457  1.217058 -1.522457  1.217058

采樣操作起來(lái)只是需要注意采樣后對(duì)新數(shù)據(jù)不同的處理方法。上面介紹的是降頻采樣。也可以升頻采樣。

# 時(shí)間頻率從天提升到小時(shí),并使用相同的數(shù)據(jù)對(duì)新增加行填充
In [23]: data.resample('H').ffill()
Out[23]:
2017-01-01 00:00:00    1.096656
2017-01-01 01:00:00    1.096656
2017-01-01 02:00:00    1.096656
2017-01-01 03:00:00    1.096656
                       ...
2017-01-19 20:00:00   -1.522457
2017-01-19 21:00:00   -1.522457
2017-01-19 22:00:00   -1.522457
2017-01-19 23:00:00   -1.522457
2017-01-20 00:00:00    1.217058
Freq: H, Length: 457, dtype: float64

# 時(shí)間頻率從天提升到小時(shí),不對(duì)新增加行填充
In [24]: data.resample('H').asfreq()
Out[24]:
2017-01-01 00:00:00    1.096656
2017-01-01 01:00:00         NaN
2017-01-01 02:00:00         NaN
2017-01-01 03:00:00         NaN
2017-01-01 04:00:00         NaN
                       ...
2017-01-19 20:00:00         NaN
2017-01-19 21:00:00         NaN
2017-01-19 22:00:00         NaN
2017-01-19 23:00:00         NaN
2017-01-20 00:00:00    1.217058
Freq: H, Length: 457, dtype: float64

# 時(shí)間頻率從天提升到小時(shí),只對(duì)新增加前3行填充
In [25]: data.resample('H').ffill(limit=3)
Out[25]:
2017-01-01 00:00:00    1.096656
2017-01-01 01:00:00    1.096656
2017-01-01 02:00:00    1.096656
2017-01-01 03:00:00    1.096656
2017-01-01 04:00:00         NaN
                       ...
2017-01-19 20:00:00         NaN
2017-01-19 21:00:00         NaN
2017-01-19 22:00:00         NaN
2017-01-19 23:00:00         NaN
2017-01-20 00:00:00    1.217058
Freq: H, Length: 457, dtype: float64
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容