時系列データで、時間に関する特徴量を作る

時系列データ

まずは、時系列データが必要です。
pandas-datareader では1分足のデータを取得することはできなさそうです。
なので、この記事を参考にして、1分足の時系列データを作成しましょう。
仮想通貨のOHLCVデータを生成する

データ期間は10日間とし、以下のようなデータを使います。

open high low close volume
timestamp
2022-07-08 12:00:00+00:00 2929782.0 2933123.0 2928366.0 2933042.0 2.78
2022-07-08 12:01:00+00:00 2932750.0 2933202.0 2930780.0 2932433.0 0.69
2022-07-08 12:02:00+00:00 2931250.0 2932750.0 2930750.0 2930750.0 2.48
2022-07-08 12:03:00+00:00 2929750.0 2929750.0 2926261.0 2927691.0 1.14
2022-07-08 12:04:00+00:00 2927858.0 2930096.0 2926402.0 2928966.0 2.06
... ... ... ... ... ...
2022-07-18 11:56:00+00:00 3058643.0 3066041.0 3056620.0 3063084.0 7.37
2022-07-18 11:57:00+00:00 3063084.0 3064973.0 3059696.0 3061920.0 1.52
2022-07-18 11:58:00+00:00 3061937.0 3061937.0 3059894.0 3060131.0 0.80
2022-07-18 11:59:00+00:00 3059931.0 3060378.0 3057600.0 3059832.0 1.58
2022-07-18 12:00:00+00:00 3060508.0 3061009.0 3058794.0 3059949.0 0.43

Index の型は datetime64[ns, UTC] となっています。
参考:pandas Timeseries overview
参考:numpy.datetime64
参考:numpy datetimes and timedeltas

Python

    print(df.index.dtype)
    # datetime64[ns, UTC]

特徴量を作る

年月日、時分秒

Python

    df['year'] = df.index.year
    df['month'] = df.index.month
    df['day'] = df.index.day
    
    df['hour'] = df.index.hour
    df['minute'] = df.index.minute
    df['second'] = df.index.second
    
    print(df.loc[:, ['year', 'month', 'day', 'hour', 'minute', 'second']])
year month day hour minute second
timestamp
2022-07-08 12:00:00+00:00 2022 7 8 12 0 0
2022-07-08 12:01:00+00:00 2022 7 8 12 1 0
2022-07-08 12:02:00+00:00 2022 7 8 12 2 0
2022-07-08 12:03:00+00:00 2022 7 8 12 3 0
2022-07-08 12:04:00+00:00 2022 7 8 12 4 0
... ... ... ... ... ... ...
2022-07-18 11:56:00+00:00 2022 7 18 11 56 0
2022-07-18 11:57:00+00:00 2022 7 18 11 57 0
2022-07-18 11:58:00+00:00 2022 7 18 11 58 0
2022-07-18 11:59:00+00:00 2022 7 18 11 59 0
2022-07-18 12:00:00+00:00 2022 7 18 12 0 0

曜日

Python

    # 曜日
    df['day_of_week'] = pd.Index(pd.Series(df.index).dt.weekday)
    print(df.loc[:, ['day_of_week']])
day_of_week
timestamp
2022-07-08 12:00:00+00:00 4
2022-07-08 12:01:00+00:00 4
2022-07-08 12:02:00+00:00 4
2022-07-08 12:03:00+00:00 4
2022-07-08 12:04:00+00:00 4
... ...
2022-07-18 11:56:00+00:00 0
2022-07-18 11:57:00+00:00 0
2022-07-18 11:58:00+00:00 0
2022-07-18 11:59:00+00:00 0
2022-07-18 12:00:00+00:00 0

週番号

Python

    # 週番号
    print(pd.Series(df.index).dt.isocalendar())
    
    df['week_number'] = pd.Index(pd.Series(df.index).dt.isocalendar().week.astype(int))
    print(df.loc[:, ['week_number']])
year week day
0 2022 27 5
1 2022 27 5
2 2022 27 5
3 2022 27 5
4 2022 27 5
... ... ... ...
14189 2022 29 1
14190 2022 29 1
14191 2022 29 1
14192 2022 29 1
14193 2022 29 1

week_number
timestamp
2022-07-08 12:00:00+00:00 27
2022-07-08 12:01:00+00:00 27
2022-07-08 12:02:00+00:00 27
2022-07-08 12:03:00+00:00 27
2022-07-08 12:04:00+00:00 27
... ...
2022-07-18 11:56:00+00:00 29
2022-07-18 11:57:00+00:00 29
2022-07-18 11:58:00+00:00 29
2022-07-18 11:59:00+00:00 29
2022-07-18 12:00:00+00:00 29

まとめ

算出した特徴量とその型を載せておきます。

Python

    # 算出した特徴量を表示する。
    features = ['year', 'month', 'day', 'hour', 'minute', 'second', 'day_of_week', 'week_number']
    print(df[features])
    
    # 各カラムの型を表示する。
    print(df.dtypes)
year month day hour minute second day_of_week week_number
timestamp
2022-07-08 12:00:00+00:00 2022 7 8 12 0 0 4 27
2022-07-08 12:01:00+00:00 2022 7 8 12 1 0 4 27
2022-07-08 12:02:00+00:00 2022 7 8 12 2 0 4 27
2022-07-08 12:03:00+00:00 2022 7 8 12 3 0 4 27
2022-07-08 12:04:00+00:00 2022 7 8 12 4 0 4 27
... ... ... ... ... ... ... ... ...
2022-07-18 11:56:00+00:00 2022 7 18 11 56 0 0 29
2022-07-18 11:57:00+00:00 2022 7 18 11 57 0 0 29
2022-07-18 11:58:00+00:00 2022 7 18 11 58 0 0 29
2022-07-18 11:59:00+00:00 2022 7 18 11 59 0 0 29
2022-07-18 12:00:00+00:00 2022 7 18 12 0 0 0 29

open float64
high float64
low float64
close float64
volume float64
year int64
month int64
day int64
hour int64
minute int64
second int64
day_of_week int64
week_number int64
dtype: object