vandelay vandelay - 5 months ago 55
Python Question

Remove non-business days rows from pandas dataframe

I have a dataframe with second timeseries data of wheat in

df
.

df = wt["WHEAT_USD"]

2016-05-02 02:00:00+02:00 4.780
2016-05-02 02:01:00+02:00 4.777
2016-05-02 02:02:00+02:00 4.780
2016-05-02 02:03:00+02:00 4.780
2016-05-02 02:04:00+02:00 4.780
Name: closeAsk, dtype: float64


When I plot the data it has theese annoying horizontal lines because of weekends. Are there any simple way of simply removing the non-business days from the dataframe itself.

Something like

df = df.BDays()

Answer

One simple solution is to slice out the days not in Monday to Friday:

In [11]: s[s.index.dayofweek < 5]
Out[11]:
2016-05-02 00:00:00    4.780
2016-05-02 00:01:00    4.777
2016-05-02 00:02:00    4.780
2016-05-02 00:03:00    4.780
2016-05-02 00:04:00    4.780
Name: closeAsk, dtype: float64

Note: this doesn't take into account bank holidays etc.