如何在9点、12点和18点在pandasper day中创建自定义频率

How to create a custom frequency in pandasper day at 9, 12 and 18 o'clock

我了解如何创建 pandas 频率,如 python3:

import pandas as pd 

import datetime

idx = pd.date_range('2017-01-01' ,'2017-06-16',  freq='D')

ts = pd.Series(range(len(idx)), index=idx)

ts

对于9点12点和18点的不规则采样小时数据,我将如何做?

你可以试试:

idx = pd.date_range('2017-01-01' ,'2017-06-16',  freq='H')
idx = idx[idx.hour.isin([9,12,18])]

ts = pd.Series(range(len(idx)), index=idx)

输出:

2017-01-01 09:00:00      0
2017-01-01 12:00:00      1
2017-01-01 18:00:00      2
2017-01-02 09:00:00      3
2017-01-02 12:00:00      4
                      ... 
2017-06-14 12:00:00    493
2017-06-14 18:00:00    494
2017-06-15 09:00:00    495
2017-06-15 12:00:00    496
2017-06-15 18:00:00    497
Length: 498, dtype: int64