Dates in Python

Working with Dates and Times in Python

Max Shron

Data Scientist and Author

Course overview

  • Chapter 1: Dates and Calendars
  • Chapter 2: Combining Dates and Times
  • Chapter 3: Time zones and Daylight Saving
  • Chapter 4: Dates and Times in Pandas
Working with Dates and Times in Python

Dates in Python

Working with Dates and Times in Python

Why do we need a date class in Python?

two_hurricanes = ["10/7/2016", "6/21/2017"]

How would you:

  • Figure out how many days had elapsed?
  • Check that they were in order from earliest to latest?
  • Know which day of the week each was?
  • Filter out hurricanes which happened between certain dates?
Working with Dates and Times in Python

Creating date objects

# Import date
from datetime import date

# Create dates two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]
Working with Dates and Times in Python

Attributes of a date

# Import date
from datetime import date

# Create dates
two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]

print(two_hurricanes_dates[0].year) print(two_hurricanes_dates[0].month) print(two_hurricanes_dates[0].day)
2016
10
7
Working with Dates and Times in Python

Finding the weekday of a date

print(two_hurricanes_dates[0].weekday())
4
  • Weekdays in Python
    • 0 = Monday
    • 1 = Tuesday
    • 2 = Wednesday
    • ...
    • 6 = Sunday
Working with Dates and Times in Python

Dates in Python

Working with Dates and Times in Python

Preparing Video For Download...