Python Tutorial: Dates in Python

Max Schrohn covers Python's datetime.date class, showing how to create date objects and extract information like weekday. He uses Florida hurricane landfalls as an example, demonstrating how weekday() returns 0 for Monday and how these basics let you calculate elapsed days and order dates.
Chapters
Hi, my name is Max Schrohn. I will be your instructor for this course on working with Dates and Times In Python, dates are everywhere in data science. Stock prices go up and down, experiments begin and end, people are born, politicians take votes, and on and on. All these events happen at a particular point in time. Knowing how to analyze data over time is a core data science skill. This course is divided into four chapters. The first chapter will be about working with dates and calendars. In chapter two, we will add time into the mix and combine dates and times. In chapter three, we'll tackle one of the toughest parts of working with time, time zones and daylight saving. And finally, in chapter four, we'll connect what we've learned about working with dates and times to explore how pandas can make answering even complex questions about dates much easier. Let's begin. Python has a special date class called Date which you will use to represent dates. A date, like a string or number or numpy array, has special rules for creating it and methods for working with it. In this lesson, we're going to discuss creating dates and extracting some basic information out of them. Why do we need a special date class? Let's have a look to understand how dates work. In this chapter, you're going to be exploring 67 years of hurricane landfalls in the US state of Florida. Two hurricanes is a list with the dates of two hurricanes represented as the last 2016 hurricane on October 7, 2016 and the first 2017 hurricane on June 21, 2017. The dates are represented in the US style with the month, then the day, then the year. Suppose you want to do something interesting with these dates. How would you figure out how many days had elapsed between them? How would you check that they were ordered from earliest to latest? How would you know which day of the week each was? Doing these things manually would be challenging, but Python makes all of them easy. By the end of this chapter, you'll know how to do each of these things yourself. To create a date object, we start by importing the Date class. The collection of date and time related classes are stored in the datetime package. We create a date using the date function. Here we've created dates corresponding to the two hurricanes. Now, as Python date objects, the inputs to date are the year, month and day. The first date is October 7, 2016 and the second date is June 21, 2017. The order is easy to remember. It goes from biggest to smallest year, month, day. Later in this chapter, you'll create dates directly from lists of strings but in this lesson, you're going to stick to creating dates by hand or using lists of already created dates. You can access individual components of a date using the date's attributes. You can access the year of the date using the year attribute, like so, and the result is 2016. Similarly, you can access the month and day using the month and day attributes, like so. You can also ask Python to do more complicated work. Here we call the weekday method on a date and see that the weekday is four. What does four mean? Here Python counts weekdays from zero starting on Monday. One is Tuesday, two is Wednesday, and so on up to six. Being a Sunday, this date was a Friday. In the next few exercises, you'll implement what you've seen in this video to see how much you can already do.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







