Charli
Charli

Reputation: 3

Select weekends and weekdays by weekday() in python

I have the following data:

2010-01-01 00:00:00 327
2010-01-01 01:00:00 237
2010-01-01 02:00:00 254
...

I can select different data by month but I would like to select data from weekdays and data from weekends. How could it be possible to do it by using weekday function? I have performed something like this but it does not work.

data['Month'] = data['Timestamp'].dt.month
summer = (data.Month >=6) & (data.Month <=8) 
data['WEEKDAY'] = ((pd.DatetimeIndex(data.index).dayofweek) // 5 == 1).astype(float)

Upvotes: 0

Views: 2787

Answers (1)

ipj
ipj

Reputation: 3598

Use:

data['WEEKDAY'] = data.Timestamp.dt.dayofweek

0 is Monday. Next mark weekends as 1 in column WEEKEND:

data['WEEKEND'] = np.where(data.Timestamp.dt.dayofweek.isin([5,6]), 1, 0)

To separate into 2 dataframes:

weekends = data[data['WEEKEND']==1]
workdays = data[data['WEEKEND']!=1]

Upvotes: 1

Related Questions