Skip to content

Instantly share code, notes, and snippets.

@paretech
Created July 10, 2020 02:44
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 paretech/b21a16d0dd194cbaf909d50783532d83 to your computer and use it in GitHub Desktop.
Save paretech/b21a16d0dd194cbaf909d50783532d83 to your computer and use it in GitHub Desktop.
import pandas as pd
def accounting_month(start, end):
month_end = pd.date_range(start=start, end=end, freq=pd.tseries.offsets.LastWeekOfMonth(n=1, weekday=6))
month_start = month_end.shift(periods=-1, freq=pd.tseries.offsets.LastWeekOfMonth(n=1, weekday=6)).shift(periods=1, freq='D')
weeks_in_month = (month_end - month_start).shift(periods=1, freq='D') / pd.Timedelta(7, 'D')
return pd.DataFrame({'start': month_start, 'end': month_end}, index=month_end.to_period('M'))
if __name__ == '__main__':
accounting_calendar = accounting_month('2019-01-01', '2022-01-01')
print(accounting_calendar['Jan 2020'])
print(accounting_calendar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment