Working with Dates and Times in Python
In Cleaning and Preparing Data In Python, you worked with simple dates: the year in which an artist produced each piece of art as well as that artist’s year of birth and death. These year values were represented as integers, which made them easy to work with. Unfortunately, working with date/time data is often a lot more complex:
- Where you have a compound date format, like January 1, 1901, separating each component value and converting it to its numeric form is cumbersome.
- There are many different formats, e.g. 12-hour time versus 24-hour time.
- Adding and subtracting across date/time boundaries isn’t easy — for instance, if I wanted to add 1 hour 35 minutes to the time 32 minutes, you’ll need to account for the fact that there are 60 minutes in an hour to be able to come up with the correct answer, 2 hours 7 minutess.
In this lesson, we will learn how to work with dates and times in Python. Python has a module called datetime that makes working with dates and times easier. In this lesson, we’ll learn how to use datetime in Python while working with a data set of visitors to the Obama White House.
Working with dates and times in Python is a vital skill because many data include date/time information, including:
- Weather data with dates and/or times.
- Computer logs with the timestamp for each event.
- Sales data with date/time range included.
- Learn to parse dates from strings using the datetime library.
- Learn techniques for data and time analysis.
- Learn how to format dates using strftime.
- Importing Modules
- The Datetime Module
- The Datetime Class
- Using Strptime to Parse Strings as Dates
- The Time Class
- Comparing Time Objects
- Calculations With Date and Time
- Summarizing Appointment Lengths
- Next Steps