Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count unique weekdays from timestamp column in dataframe in Python

I would like to count how many unique weekdays exist in timestamp. Here's an input and I want output to be 4(since 8/5 and 8/6 are weekends).

    captureTime
0   8/1/2017 0:05
1   8/2/2017 0:05
2   8/3/2017 0:05
3   8/4/2017 0:05
4   8/5/2017 0:05
5   8/6/2017 0:05
like image 495
ejshin1 Avatar asked Jan 04 '23 13:01

ejshin1


2 Answers

Using np.is_busday:

import numpy as np
import pandas as pd
df = pd.DataFrame( {
    'captureTime':[ '8/1/2017 0:05', '8/2/2017 0:05', '8/3/2017 0:05', 
                    '8/4/2017 0:05', '8/5/2017 0:05', '8/6/2017 0:05']})
df['captureTime'] = pd.to_datetime(df['captureTime'])

print(np.is_busday(df['captureTime'].values.astype('datetime64[D]')).sum())

prints

4

Above, all business days are counted once. If you wish to count identical datetimes only once, you could use

np.is_busday(df['captureTime'].unique().astype('datetime64[D]')).sum()

Or, if you wish to remove datetimes that have identical date components, convert to datetime64[D] dtype before calling np.unique:

np.is_busday(np.unique(df['captureTime'].values.astype('datetime64[D]'))).sum()
like image 155
unutbu Avatar answered Jan 28 '23 17:01

unutbu


One way is pandas series.dt.weekday

df['captureTime'] = pd.to_datetime(df['captureTime'])
np.sum(df['captureTime'].dt.weekday.isin([0,1,2,3,4]))

It returns 4

You can use boolean indexing in case you need to capture the dates

df[df['captureTime'].dt.weekday.isin([0,1,2,3,4])]

    captureTime
0   2017-08-01 00:05:00
1   2017-08-02 00:05:00
2   2017-08-03 00:05:00
3   2017-08-04 00:05:00
like image 35
Vaishali Avatar answered Jan 28 '23 15:01

Vaishali