Python Dates and Times Video Lecture Transcript This transcript was automatically generated, so there may be discrepancies between the video and the text. Hi everybody, welcome back in this video we're going to talk about time and dates in Python. So we're gonna talk about how Python handles dates and times. So we'll introduce the date time module, time deltas, date times and Numpy as well as timestamps and pandas.So this video is sort of optional. If you feel like you'd like to go through it and then see some of these things, or you're unfamiliar with these objects in Python, it might be useful since we're working with time series data. But if not, feel free to move along to the next video where we go back to time series and forecasting.So Python has a few different ways that we can handle dates and times and date times. The first are some built in ways. So Python has a built in module for handling time and date data. This is the date time module whose documentation I've provided here as a link. So this module allows you to make date objects which only include the dates.They allow you to make date time objects, which involves both the date and the time. They also allow you to make time deltas which encode a temporal difference between two dates or two date time. So for instance, as we'll see, you can take a date time. So let's say it's March 5th, 1992, then you can add 70 days to it and then see what the date time would be. From there we're going to focus on date times and time deltas, and then if you're interested, you can learn more about dates later.So there are a couple different ways we can make a date time from scratch. This is the most basic. So you call date time dot date time. So this is the date time modules date time object. And then you put in times and dates in the following order. So first you put the year, then the month, then the day, then hour, minute, 2nd, and microsecond. So we're going to do, let's see 2022.And let's do 411. So this is what a date time object looks like when it's displayed. So now go ahead and store your own date time. And I've written a suggestion here that you can put a date time for your birthday. I'm going to keep my birthday a secret and put in the following date time and store it. So once you have a date time that's stored, you can access the individual times and dates numbers by.Doing a dot and then whatever piece of information you'd like to access. So for instance you can get the year with dot year, the month with dot month, the day with dot day, the hour with dot hour and so on. The date time module also has very nice specific date times like date time dot now, so we have to do date time dot date time. Sorry about that.And So what this will do is it's going to give you the precise time that it is according to your Q, according to your computer's internal clock. So at the time when I ran this code, it was April 20th, 2022, the 16th hour. So it works on, you know, military time, the 31st minute of the hour, the 26th, 2nd. And this is the microsecond.Time Delta allows you to move forward or backward from a given date time. So here is the current date time that I've stored in capital D and then I can move back by let's say two days by doing date time dot time delta days equals two, so you know current date time. This was the the D oh that's right, that's the one I chose. This was April 11th and then two days before April 11th was April 9th.So you can do this not just for days, but you can do it for a week. So we could put in weeks equals two and it would take care of that for me. Hours, minutes, seconds, microseconds and milliseconds. And these are the. These are the words that you would use. So for instance, if I wanted minutes, I would put in minutes equals. If I wanted microseconds I would put in microseconds equals and so forth.So Numpy maybe you're working in a data set that's been loaded in with Numpy. Numpy also has its own built in date time 64 and time Delta 64 objects that you might use. It really just depends on the project. So normally when I'm doing it I'm using either date time from Python or date time from pandas. Those are my preferred. I don't really like the pandas one, but you have to use it if you're using pandas.But sometimes you may have to also know how to use date time 64 and time Delta 64. So the easiest way to make a date time and Numpy with date time 64 is to do NP dot date time 64 and then you put it in as a string so you can do the year, so 2022 the month, and then dash the day and then. I believe you can also add in some time arguments there, but you'll have to check the documentation to be sure.Which I think I'm now realizing I didn't put the documentation in yet. So I will put the documentation in your version that you're going to see. But I haven't put it in my version yet. So date Time 64 is not as nice because you're not able to access the day, month or year on its own. So there's no like date time 64 dot year returning what the year is, but date time 64 time deltas, they do work in the same way so for instance.We can move ahead, you know, 12 days into the future by doing NP dot time delta 64. Here you specify the amount of time and then, the unit. So in Numpy Capital D is for days, capital W is for weeks, Capital M is for months, Capital Y is for years, little H is for hours, little M is for minutes and little S is for seconds. And a nice feature.A nice feature about date time 60 fours is they do play nicely with like NP arranged so you can make an array of date of date time 60 fours going from one day to another day and intervals given by the unit. So let's say I want to go from March 24th to April 17th by days and when I do this I would get that back and so here it's stored as. It looks like they're strings but.That you can see from the D type it is a date time. Okay, so I did link to the documentation. Way to go past Matt. So here we have the documentation for all of this stuff. Again, I typically don't use these but some people really like working in Numpy cuz it's fast computationally I believe.