dandroid
dandroid

Reputation: 77

Find date ranges that include the weekend using Pandas

I have a pandas DataFrame in Python with two columns denoting start and end date. I want to:

The dataset looks like:

start       end
2013-08-02  2013-08-04
2014-11-24  2014-11-28
2013-10-29  2013-10-31
2013-12-06  2013-12-10
2014-08-15  2014-08-17

I would be expecting something like:

has_weekend
TRUE
FALSE
FALSE
TRUE
TRUE

My current approach is quite slow for a DataFrame that has close to 2M rows. Here's the code:

df.apply(lambda x: np.any(np.in1d([d.weekday() for d in pd.date_range(x.start, x.end)],[4,5])), axis=1)

Any ideas?

SOLUTION The fastest solution was a modified answer from @Anton VBr 's

s = df.start.dt.dayofweek
e = df.end.dt.dayofweek
dt = (df.end- df.start).dt.days
has_weekend = ( ((s >= 4) & (s != 6)) | ( e>=5) | ((e < s) & (s != 6)) | (dt >= 6) )

Upvotes: 1

Views: 1021

Answers (1)

Anton vBR
Anton vBR

Reputation: 18906

I thought about some logical operators and these should do, however they aren't any time improvement on the small set I tested.

s = df.start.dt.dayofweek
e = df.end.dt.dayofweek
(((s >= 4) & (s != 6)) | (( e>=4) & (s != 6)) | (e < s))

Upvotes: 2

Related Questions