Skip to content

Instantly share code, notes, and snippets.

@sakurai-youhei
Last active December 3, 2020 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakurai-youhei/bca4d8854597391c5c17a840b511f51f to your computer and use it in GitHub Desktop.
Save sakurai-youhei/bca4d8854597391c5c17a840b511f51f to your computer and use it in GitHub Desktop.
Consecutive 3+ daysoff in Pandas
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