Question

I am trying to use bdate_range with '1min' freq to get minute by minute data on all business days.

df = pd.bdate_range('20130101 9:30','20130106 16:00',freq='1min')

with output ends with

......
2013-01-05 23:59:00
2013-01-06 00:00:00

In [158]: 

Notice that 2013-01-05 and 2013-01-06 are weekends and it didn't take time limit between 9:30 and 16:00

I think the freq = '1min' totally overwrites freq = 'B' from function name bdate_range

I also tried using date_range. It worked for the time range from 9:30 to 16:00, but it can't exclude weekends.

Thanks!

Was it helpful?

Solution

You could do it like this

In [28]: rng = pd.date_range('2012-01-01', '2013-01-01', freq="1min")

In [29]: rng
Out[29]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-01 00:00:00, ..., 2013-01-01 00:00:00]
Length: 527041, Freq: T, Timezone: None

Limit the times that I want

In [30]: x = rng[rng.indexer_between_time('9:30','16:00')]

In [31]: x
Out[31]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-01 09:30:00, ..., 2012-12-31 16:00:00]
Length: 143106, Freq: None, Timezone: None

Only days that are mon-fri

In [32]: x = x[x.dayofweek<5]

In [33]: x
Out[33]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-02 09:30:00, ..., 2012-12-31 16:00:00]
Length: 102051, Freq: None, Timezone: None
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top