<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Pandas處理時間序列的數(shù)據(jù)

          共 3301字,需瀏覽 7分鐘

           ·

          2021-07-10 05:13

          在進行金融數(shù)據(jù)的分析以及量化研究時,總是避免不了和時間序列的數(shù)據(jù)打交道,常見的時間序列的數(shù)據(jù)有比方說一天內(nèi)隨著時間變化的溫度序列,又或者是交易時間內(nèi)不斷波動的股票價格序列,今天小編就為大家來介紹一下如何用“Pandas”模塊來處理時間序列的數(shù)據(jù)

          01

          創(chuàng)建一個時間戳
          首先我們需要導(dǎo)入我們所需要用到的模塊,并且隨機創(chuàng)建一個時間戳,有兩種方式來創(chuàng)建,如下所示
          import pandas as pdimport numpy as npfrom datetime import datetime
          pd.to_datetime('2021-05-20')  ##output: Timestamp('2021-05-20 00:00:00')pd.Timestamp('2021-05-20')  ##output: Timestamp('2021-05-20 00:00:00')
          02

          訪問時間信息
          我們可以來查看一下這個時間戳所代表的年、月、日等信息
          a = pd.Timestamp('2021-10-01')a.day_name()    ## Friday,看來今年的10月1日是周五哈?a.month_name()  ## October 十月份a.day(), a.month(), a.year()  ## 1, 10, 2021,查看年月日等信息
          03

          數(shù)據(jù)格式轉(zhuǎn)化為時間序列
          接下來我們做一些數(shù)據(jù)處理,例如將數(shù)據(jù)集中的“time_frame”轉(zhuǎn)化為時間序列的格式
          df = pd.DataFrame({"time_frame": ["2021-01-01", "2021-01-02", "2021-01-03", "2021-01-04", "2021-01-05"]})df['time_frame'] = pd.to_datetime(df['time_frame'])

          04

          字符串轉(zhuǎn)化成時間格式
          要是我們想將里面的時間序列的數(shù)據(jù)變成字符串時,可以這么來操作
          date_string = [str(x) for x in df['time_frame'].tolist()]

          當(dāng)然從字符串轉(zhuǎn)換回去時間序列的數(shù)據(jù),在“Pandas”中也有相應(yīng)的方法可以來操作,例如
          time_string = ['2021-02-14 00:00:00', '2021-02-14 01:00:00', '2021-02-14 02:00:00', '2021-02-14 03:00:00', '2021-02-14 04:00:00', '2021-02-14 05:00:00', '2021-02-14 06:00:00']pd.to_datetime(time_string, infer_datetime_format = True)

          當(dāng)然我們還有其他的方式來實現(xiàn)將字符串轉(zhuǎn)換成時間格式,例如
          import datetimetext_1 = "2021-02-14"datetime.datetime.strptime(text_1, '%Y-%m-%d')
          05

          提取時間格式背后的信息
          在時間序列的數(shù)據(jù)處理過程當(dāng)中,我們可能需要經(jīng)常來實現(xiàn)下面的需求

          l求某個日期對應(yīng)的星期數(shù)(2021-06-22是第幾周)

          l判斷一個日期是周幾(2021-02-14是周幾)

          l判斷某一日期是第幾季度,等等

          當(dāng)數(shù)據(jù)集中的某一列已經(jīng)轉(zhuǎn)化為是“datetime64”的格式時,僅需要用到“dt”的方法,就可以快速得到相應(yīng)的結(jié)果,例如
          df = pd.DataFrame({"time_frame": ["2021-01-01", "2021-01-02", "2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06", "2021-01-07", "2021-01-08"]})df["time_frame"] = pd.to_datetime(df["time_frame"])# 一周中的第幾天df.time_frame.dt.dayofweek[0]# 返回對應(yīng)額日期df.time_frame.dt.date[0]# 返回一周中的第幾天,0對應(yīng)周一,1對應(yīng)周二 df.time_frame.dt.weekday[0]
          除此之外,下表列出了幾個并不常見的方法和屬性

          06

          關(guān)于date_range函數(shù)
          可用于創(chuàng)建時間索引,并且時間頻率可以靈活調(diào)整,參數(shù)“freq”就是用來調(diào)整時間頻率的,“M”代表月份,“D”就代表的是天數(shù)了
          pd.date_range(start='2021-02-14', periods=10, freq='M')

          07

          period_range和timedelta_range函數(shù)
          和上面的“date_range”函數(shù)類似,用于創(chuàng)建時間索引
          pd.period_range('2021', periods=10, freq='M')

          pd.timedelta_range(start='0', periods=24, freq='H')

          08

          關(guān)于重采樣resample
          我們也可以對時間序列的數(shù)據(jù)集進行重采樣,重采樣就是將時間序列從一個頻率轉(zhuǎn)換到另一個頻率的處理過程,主要分為降采樣和升采樣將高頻率、間隔短的數(shù)據(jù)聚合到低頻率、間隔長的過程稱為是降采樣,反之則是升采樣.

          我們先來創(chuàng)建一個包含30個值和一個時間序列索引的數(shù)據(jù)集
          A = pd.date_range('2021-01-01', periods=30, freq='D')values = np.random.randint(10, size=30)S = pd.Series(values, index=A)
          返回5天時間內(nèi)的數(shù)據(jù)加總
          S.resample('5D').sum()

          09

          關(guān)于滑動窗口“rolling”和“expanding”
          因此便就有了滑動窗口這一個概念,簡而言之就是將某個時點的數(shù)據(jù)衍生到包含這個時點的一段時間內(nèi)做一個數(shù)據(jù)統(tǒng)計。首先我們先來創(chuàng)建需要用到的數(shù)據(jù)集
          index = pd.date_range('2021-01-01',periods=30data = pd.DataFrame(np.arange(len(index)),index=index,columns=['test'])

          主要有“rolling”方法和“expanding”方法,“rolling”方法考慮的是一定的時間段內(nèi)的數(shù)據(jù),而“expanding”考慮的則是之前所有的數(shù)據(jù),例如

          # 移動3個值,進行求和data['sum'] = data.test.rolling(3).sum()# 移動3個值,進行求平均數(shù)data['mean'] = data.test.rolling(3).mean()

          我們發(fā)現(xiàn)數(shù)據(jù)集中有一些缺失值,我們這里就可以使用“pandas”中特有的方法來進行填充,例如
          data['mean'].fillna(method = 'backfill')



          相關(guān)閱讀:


          瀏覽 59
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  东京热AV无码国产东京热AⅤ | 日韩黄色视频频在线播放 | 国产V视频在线观看 | 欧美福利在线视频 | 日韩中文在线视频 |