Dates and times

1. Adding time to the mix

In this chapter, you are going to move from only working with dates to working with both dates and times: the calendar day AND the time on the clock within that day.

2. Dates and Times

As always, let's start with an example. Here is an example of a date and a time together: October 1, 2017, at 3:23:25 PM. Unlike before, where we were only working with the date, we're now going to also include the time. Let's see how to represent this in Python.

3. Dates and Times

The first thing we have to do is import the datetime class from the datetime package. Ideally, these would have different names, but unfortunately for historical reasons they have the same name. This is just something to get used to.

4. Dates and Times

We're going to create a datetime called "dt" and populate the fields together.

5. Dates and Times

The first three arguments to the datetime class are exactly the same as the date class. Year, then month, then day, each as a number.

6. Dates and Times

Next, we fill in the hour. Computers generally use 24 hour time, meaning that 3 PM is represented as hour 15 of 24.

7. Dates and Times

We put in the minutes, 23 out of 60.

8. Dates and Times

And finally, the seconds. October 1, 2017 at 3:23:25PM is represented as a datetime in Python as 2017, 10, 1, 15, 23, 25). All of these arguments need to be whole numbers; if you want to represent point-5 seconds,

9. Dates and Times

you can add microseconds to your datetime. Here we've added 500,000 microseconds, or point-5 seconds. That is, Python breaks seconds down into millionths of a second for you when you need that kind of precision. If you need billionths of a second precision (which happens sometimes in science and finance) we'll cover nanoseconds when we get to Pandas at the end of this course. Python defaults to 0 microseconds if you don't include it.

10. Dates and Times

That's a lot of arguments; if it helps, you can always be more explicit and use named arguments.

11. Replacing parts of a datetime

We can also make new datetimes from existing ones by using the replace() method. For example, we can take the datetime we just made, and make a new one which has the same date but is rounded down to the start of the hour. We call dt-dot-replace() and set minutes, seconds, and microseconds to 0. This creates a new datetime with the same values in all the other fields, but these ones changed.

12. Capital Bikeshare

Before we wrap up, let's talk about the data we will use for the rest of this course. You will be working with data from Capital Bikeshare, the oldest municipal shared bike program in the United States. Throughout the Washington, DC area, you will find these special bike docks, where riders can pay to take a bike, ride it, and return to this or any other station in the network. We will be following one bike, ID number "W20529", on all the trips it took in October, November, and December of 2017. Each trip consisted of a date and time when a bike was undocked from a station, then some time passed, and the date and time when W20529 was docked again.

13. Adding time to the mix

In this video, we walked through how to create datetime objects in Python. You're going to practice that in the exercises, and also work with the Capital Bikeshare data and see how we can use Python to understand the trips that W20529 took throughout the three months we're interested in.