A datetime is a data type for storing date and time. It can model a specific date (such as October 4, 2021), a particular time (such as 11:50 a.m.), or both (such as October 4, 2021 at 11:50 a.m.). Datetimes are valuable because they allow us to track trends over time. A financial analyst may use datetimes to determine the weekdays when a stock performs best. A restaurant owner may use them to discover the peak hours that customers are patronizing the business. An operations manager may use them to identify the parts of a process that are creating bottlenecks in production. The when in a data set can often lead to the why.
In this chapter, we’ll review Python’s built-in datetime objects and see how pandas improves them with its Timestamp and Timedelta objects. We’ll also learn how to use the library to convert strings to dates, add and subtract offsets of time, calculate durations, and more. There’s no time to waste (pun intended), so let’s dive in.