Accounting for unique weekdays from a timestamp column in a dataframe in Python

I would like to calculate how many unique weekdays exist in a timestamp. Here is the input and I want the 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
+4
source share
4 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 working days are counted once. If you want to count identical datetimesonly once, you can use

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

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

np.is_busday(np.unique(df['captureTime'].values.astype('datetime64[D]'))).sum()
+3
source

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]))

He returns 4

,

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
+3

Convert to a date using pd.to_datetime, get a unique list of daily diaries, and count everything less than 5.

out = (df.captureTime.apply(pd.to_datetime).dt.dayofweek.unique() < 5).sum()
print(out)

4

df.uniqueremoves duplicates, leaving you with a unique array daysofweekon which instances are displayed in 5( 0 - 4→ weekdays).


Conclusion df.dayofweek:

out = df.captureTime.apply(pd.to_datetime).dt.dayofweek
print(out)

0    1
1    2
2    3
3    4
4    5
5    6
Name: captureTime, dtype: int64
+1
source

Assuming you have captureTime as a datetime object, you can do this,

s = df['captureTime'].dt.weekday s[s >= 5].count() # 5, 6 corresponds to saturday, sunday

+1
source

Source: https://habr.com/ru/post/1684626/


All Articles