Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find all the Fridays of the year 2019 using Python
________ ________ ___ __ ___
|\_____ \|\ __ \|\ \|\ \ |\ \
\|___/ /\ \ \|\ \ \ \/ /|\ \ \
/ / /\ \ __ \ \ ___ \ \ \
/ /_/__\ \ \ \ \ \ \\ \ \ \ \
|\________\ \__\ \__\ \__\\ \__\ \__\
\|_______|\|__|\|__|\|__| \|__|\|__|
You don't have to use packages such as datetime, timedelta, calendar.
Here I give you a more simple way using pandas. This will drastically shorten your program length.
Ignore Pycharm, just try it on your favorite notebook.

Dates estimator using Python

Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.

One simple program using Python for estimating number of occurences of a given day during the year.
E.g:

  • use this program if you want to estimate a specific day of a given year.
  • in our program we'll try to guess all fridays of the year 2019
  • Refer to dates_estimator.py for the program and to exercise_solution.py for a solution

Getting Started

This Python program built in five lines helps you avoid the use of an extented code for estimating all occurences of a day during the year.

  • (#) and (''') are used to comment the following gist.

Prerequisites

I am using Jupyter Notebook on localhost (Ubuntu 18.04 bionic).
Make sure to have Jupyter Notebook installed on your operating system or launch it on remote servers (see Tips).

Tips

If you are not using Linux/Unix and still want to try this simple Python program:

Basic commands in Jupyter Notebook

  • Note that in Jupyter you add new lines by typing "b" from your keyboard whilst the notebook is opened.
  • Avoid runing the entire code in a single cell in order to understand the steps.
  • Use "ctrl + enter" to execute each line if you want to get the output.
  • Use "dd" outside a cell to delete it.
  • Use "a" outside a cell to add a new cell above it.
  • Use "b" outside as cell to add a new cell below it.
  • Running the last cell should execute the permutations as program output.

Whilst your Jupyter Notebook is open... Use this line of code in your first cell

import pandas as pd

'''
This loads requested package
and library in your notebook
'''

Use this lines of code in your second cell

def allfridays(year):
    return pd.date_range(start=str(year), end=str(year+1),
                         freq='W-FRI').strftime('%m/%d/%Y').tolist()

allfridays(2019)[:52]

'''
This will print out all fridays of the year 2019. Note that we've set the program to run on 52 weeks of a regular year. You can set the program to any number of weeks or to another year.
'''

Running the tests

  • I used Ubuntu (18.04 bionic) to launch Jupyter Notebook on localhost.
  • Localhost instantiates while using $ jupyter notebook in the terminal.
  • Check if Jupyter is correctly installed: $ jupyter --version

Built With

  • Jupyter - An open source software for creating notebooks
  • Pandas - A high-level data manipulation tool developed by Wes McKinney

Versioning

I used no vesioning system for this gist, which repos status is flagged as concept because it is intended to be a demo or POC (proof-of-concept).

Author

  • Isaac Arnault - Suggesting a minified code from Initial work zhenyi2697

License

All public gists https://gist.github.com/isaacarnault
Copyright 2018, Isaac Arnault
MIT License, http://www.opensource.org/licenses/mit-license.php

Exercise

Write a simple Python programm that outputs:

  • full date and time of the current day (as shown by your Linux shell using $ date)
  • current year
  • current month
  • current week number
  • current day in the current week
# Full program
import datetime
date = datetime.datetime.now()
date.year
date.month
weekNumber = date.today().isocalendar()[1]
date.day
print(date)
print(date.year)
print(date.month)
print(weekNumber)
print(date.day)

Ignore below outputs if programs ran correctly.
If you begin in Python using Jupyter these are what you should get:

Program

# Full program
import pandas as pd

def allfridays(year):
    return pd.date_range(start=str(year), end=str(year+1),
                         freq='W-FRI').strftime('%m/%d/%Y').tolist()
allfridays(2019)[:52]

isaac-arnault-dates-estimator-using-python.png

Exercise solution

# Full program
import datetime

date = datetime.datetime.now()
date.year
date.month
weekNumber = date.today().isocalendar()[1]
date.day

print(date)
print(date.year)
print(date.month)
print(weekNumber)
print(date.day)

isaac-arnault-exercise-solution.png

# 1. Import pandas
import pandas as pd
# 2. Define a function and return pd as a date range. For fridays, use FRI, for sundays, use SUN
def allfridays(year):
return pd.date_range(start=str(year), end=str(year+1),
freq='W-FRI').strftime('%m/%d/%Y').tolist()
# 3. Tell the function to execute on a given year and to apply its results on a number of weeks
allfridays(2019)[:52]
# Full program
import pandas as pd
def allfridays(year):
return pd.date_range(start=str(year), end=str(year+1),
freq='W-FRI').strftime('%m/%d/%Y').tolist()
allfridays(2019)[:52]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.