Created
May 9, 2020 21:44
-
-
Save yy/a0757a219f8aaf6d3ebaf91a38001639 to your computer and use it in GitHub Desktop.
A simple script to get the list of Mondays, Tuesdays, etc. for a given date period.
This file contains 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
#!/usr/bin/env python | |
"""Print the dates of certain day of week given date range.""" | |
import argparse | |
from datetime import date | |
import pandas as pd | |
def parse_dayofweek(dow): | |
"""Return pandas frequency string from day of the week name.""" | |
dow_dict = { | |
"sun": "W-SUN", | |
"mon": "W-MON", | |
"tue": "W-TUE", | |
"wed": "W-WED", | |
"thu": "W-THU", | |
"fri": "W-FRI", | |
"sat": "W-SAT", | |
"sunday": "W-SUN", | |
"monday": "W-MON", | |
"tuesday": "W-TUE", | |
"wednesday": "W-WED", | |
"thursday": "W-THU", | |
"friday": "W-FRI", | |
"saturday": "W-SAT", | |
} | |
return dow_dict[dow] | |
def all_days(dow, start_date, end_date): | |
"""Create a date range for certain day of the week.""" | |
return ( | |
pd.date_range(start=start_date, end=end_date, freq=dow) | |
.strftime("%m/%d") | |
.tolist() | |
) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Return all dates (e.g. Mondays).") | |
parser.add_argument( | |
"dow", | |
default="mon", | |
nargs="?", | |
help="Day of the week, e.g. 'Mon' or 'Monday'. The default value is Monday.", | |
) | |
parser.add_argument( | |
"-e", | |
"--end_date", | |
default=date(date.today().year, 12, 31).isoformat(), | |
help="Last date. e.g. '2020-12-31' The default is the end of this year.", | |
) | |
parser.add_argument( | |
"-s", | |
"--start_date", | |
default=date.today().isoformat(), | |
help="The first date. e.g. '2020-01-01'. The default is today.", | |
) | |
args = parser.parse_args() | |
for x in all_days( | |
parse_dayofweek(args.dow.lower()), args.start_date, args.end_date | |
): | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment