用熊猫日期范围功能,但跳过周末天创建日期指数(周六,周日)

问题描述 投票:1回答:1

我试图在一系列订购一些股票价格,并想设置相应的日期为指标。我不喜欢的东西,要创建索引:

date_index = pd.date_range('2018-01-01', periods = 30, freq = 'D')

事情是,我的价格列表跳过本周结束日期,切莫周六和周日的考虑。

我怎么能创造也跳过周六,周日的指数?

pandas date date-range
1个回答
1
投票

使用weekday与过滤:

date_index = pd.date_range('2018-01-01', periods = 30, freq = 'D')
print (date_index[date_index.weekday < 5])
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
               '2018-01-05', '2018-01-08', '2018-01-09', '2018-01-10',
               '2018-01-11', '2018-01-12', '2018-01-15', '2018-01-16',
               '2018-01-17', '2018-01-18', '2018-01-19', '2018-01-22',
               '2018-01-23', '2018-01-24', '2018-01-25', '2018-01-26',
               '2018-01-29', '2018-01-30'],
              dtype='datetime64[ns]', freq=None)

如果想与DatetimeIndex过滤行:

print (df[df.index.weekday < 5])
© www.soinside.com 2019 - 2024. All rights reserved.