Last active
December 3, 2020 08:25
-
-
Save sakurai-youhei/bca4d8854597391c5c17a840b511f51f to your computer and use it in GitHub Desktop.
Consecutive 3+ daysoff in Pandas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
series = daysoff.astype(int).to_series() | |
delta_2days = pd.Timedelta(2, unit='d').to_timedelta64().astype(float) | |
consecutive_3days = lambda S: abs(S.iloc[0] - S.iloc[-1]) == delta_2days | |
start = series.iloc[::-1].rolling(3).apply(consecutive_3days)[::-1].fillna(0) | |
middle = series.rolling(3, center=True).apply(consecutive_3days).fillna(0) | |
end = series.rolling(3).apply(consecutive_3days).fillna(0) | |
consecutive_3plus_daysoff = daysoff[(start + middle + end).astype(bool)] # 期間中の3連休以上 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment