0:00
hello world have you ever wanted to
0:01
learn how to use python's datetime
0:03
module well if you have you've come to
0:04
the right place today we're going to
0:06
cover everything that you need to learn
0:08
as a new programmer budding data
0:09
scientist or maybe even future algo
0:12
trader so what are we going to cover
0:13
today well we're going to start off easy
0:15
first we're going to learn how to import
0:17
date time and also install and import pi
0:19
dz which we'll use to make our time
0:24
second we'll then cover creating moments
0:26
in time including dates times and date
0:28
times and then understand the difference
0:31
third we're going to then create time
0:33
deltas which are durations in time which
0:36
we typically use to add or subtract from
0:38
moments in time such as you know two
0:42
and then after we have an understanding
0:43
of both times and time deltas we're
0:46
going to look at the various attributes
0:48
of both and you'll see why when we cover
0:50
this in the jupiter notebook and then
0:52
finally we'll wrap up how to make our
0:54
data time zone aware using pi tz so i
0:57
hope you're as excited as i am let's go
0:59
ahead and create some code
1:01
so the first thing that we're going to
1:02
want to do is make sure that we have a
1:04
virtual environment activated if you're
1:05
not sure what a virtual environment is
1:07
that's okay you can check this video in
1:08
the card right here which will show you
1:10
how to install and activate the virtual
1:12
environment but assuming you have a
1:14
virtual environment activated the first
1:16
thing that we're going to want to do is
1:18
itz into that virtual environment you
1:20
can do that by typing bang tip which
1:24
bang this allows you to
1:26
run any command pip install itv if
1:29
you're running this from the command
1:30
line you won't need the bang and you hit
1:33
enter and notice i already have this
1:35
requirement satisfied if you haven't had
1:37
python cd installed yet you'd get a
1:40
different message so now we can delete
1:42
that and we can now grab the imports
1:44
that we need for this lesson we'll do
1:46
import date time and import
1:49
itz and then hit enter i'm going to
1:51
create some headings so that way i can
1:53
upload this jupyter notebook with some
1:55
comments so that way you can either
1:56
follow along with this video or follow
1:58
along on your own time i can go ahead
2:01
and learn how to do this stuff so do
2:06
times and then we'll create a date
2:10
a date equals date time which is the
2:12
module name and this can get a little
2:14
confusing because there's both
2:15
datetime.date datetime.datetime and
2:17
date.time but i'll cover that as we go
2:20
so we'll create a new date but the year
2:22
9 30 which was the original version of
2:25
this blog article which is now being
2:27
turned into a youtube video because it
2:28
makes more sense and then we'll just
2:30
type date here and we can see that we've
2:33
created a datetime.date
2:35
when we print it we'll actually get a
2:36
different output it might be more i
2:39
guess uh common a common
2:41
way to see this date when we see 2019
2:43
930 but pretty self-explanatory there
2:47
and now we can also create a time that
2:49
time is simply uh you know obviously you
2:52
can think of a date the time is just an
2:53
hour of the day i want to do time equals
2:58
time i'm gonna do six
3:00
thirty how about nine seconds one two
3:04
which will be the milliseconds we can do
3:06
the same thing with print time
3:08
we can see six thirty nine seconds and
3:11
then here's our milliseconds and then a
3:15
the same thing um a date and time just
3:18
added together so we'll do dt
3:20
so that way we don't overwrite the name
3:22
of our datetime monitor right so if i
3:26
equals and you can see up here we've
3:28
imported datetime so that would cause us
3:30
some problems we'll just do dt equals
3:32
date time date time and that seems a
3:35
little funky at first because you're you
3:37
know you're calling date time from date
3:41
how it is and then we can just do the
3:43
same exact thing in this case we'll do
3:45
the date will be uh 2019 9 30 and then
3:48
i'll do the time will be 6 30 9 seconds
3:53
no specific reason why and i'll print dt
3:57
and we can see now we have both the date
4:00
and the time component so again a date
4:04
is just you know a date and time and
4:05
time is just the hour day and the date
4:07
time is the same thing only combined and
4:11
they are as we'll see different types so
4:14
watch this we'll do type date
4:17
it's a date time date type
4:21
we can see it's a date time time and
4:24
type dt we can see that's a date time so
4:27
just be aware that those are different
4:30
types perfect now let's go ahead and see
4:33
if we can get the current day
4:40
is pretty easy we can simply go or type
4:49
and we should know already
4:51
that today is a date and will not have a
4:55
time component as we can see there and
4:57
we can do the same thing for now which
5:06
eight time now right and you should have
5:10
caught that error almost immediately
5:11
because you know now has a time
5:13
component so we need to use date time
5:16
not date so print now
5:22
we now have the time component so that's
5:27
and we don't just have to create date
5:29
times by entering in the you know
5:33
and you know calling today or now we can
5:35
also create date times and times from
5:40
and that's actually a very common method
5:41
so we'll do that now but first let's
5:44
create a datetime string so we'll type
5:48
spring equals let's say 10
5:57
right and you can see that's just sort
5:59
of an arbitrary uh string we can parse
6:03
part time equals date time
6:08
strp time so that's the key right so
6:12
we're using date time because we are
6:14
dealing with times and dates and date
6:16
time right so we need to use the date
6:18
time from daytime and then we parse the
6:21
string so that the p there's all right
6:23
here there's also strf time which takes
6:27
times and formats it into a string but
6:29
just wanted to make sure you understand
6:33
is there's a p which parses times and
6:35
then f which formats times and then we
6:40
uh format code which i'll show you in a
6:45
then d for day percent y and then
6:56
oh that would be yeah that'd be y
7:01
i really butchered that all right date
7:07
okay perfect so now what we're doing is
7:09
we're calling this strp time or parse
7:14
right using this format code
7:22
and time and we can actually see the
7:24
list of these right so we can actually
7:27
get pretty complex and we will in a
7:28
second but you can see there's there's a
7:30
lot of them so we can see that i use
7:33
percent x which is the vocals
7:34
appropriate time representation
7:37
right or i could have done any any
7:39
number of other options so
7:42
let's actually do one that's a little
7:45
we'll just call it uh
7:47
date time spring again
7:49
this time will be thursday
7:57
and we'll just call this more complex
8:02
scrp time over parsing
8:07
ring and i'll create a new row here uh
8:10
simply our carriage returns to give us a
8:20
and a two letter here i
8:25
hopefully i got that right the prince
8:28
more complex which should now be a
8:37
and then we can see now it is now a date
8:40
time we've converted that from this more
8:42
complicated string and then printed more
8:45
complex to show that you know it is now
8:48
a date time and really it's it's not
8:50
complex it's just understanding
8:53
and tracking down how the format is and
8:55
making sure that you know you don't
8:57
forget this comma here because there is
9:00
a comma in this uh this string so just
9:03
understanding how those format strings
9:07
now that we understand how to create
9:09
moments in time either either directly
9:12
or using a method such as today or now
9:15
or by parsing a complicated string now
9:18
let's figure out how to create
9:21
durations and time or time deltas right
9:27
and we can either import
9:33
which we will do or you can just call it
9:35
from date time so we'll see
9:38
both methods so days delta equals
9:41
date time dot time delta and we'll do
9:45
days equal five which means five days in
9:53
days delta what do we get well we know
9:58
and we know five days from now will be
10:02
right so this will actually give us uh
10:05
you know christmas or 12 25 uh where i'm
10:10
now that's uh in the future we could
10:13
gone in the past right i'll do
10:20
and that would have been the 15th or
10:22
five days ago but we don't just have to
10:24
work with dates we can work with things
10:26
that are much more granular so we'll
10:27
just do all delta you'll see my point
10:30
i'll about in a second we'll do time
10:33
delta because we don't need to use you
10:36
datetime.time delta because we've
10:38
already imported a time delta from this
10:41
module so let's go ahead and do days
10:49
one you get the idea
11:00
and then we'll go ahead and
11:04
and see what we see here so we can see
11:10
automatically for us right
11:12
we know the days of eight as weeks is
11:15
one plus one day right the second that
11:18
figured it out and then the microseconds
11:21
um also so that's you know so even
11:25
adjusted all of these it actually
11:30
so to speak so anyways let's see what
11:32
happens now when we add this to now so
11:35
we'll do future equals now plus all
11:38
delta and then future
11:40
we can see that it's the 28th that you
11:43
know that time so we pushed everything
11:48
and that's pretty pretty much it from a
11:51
time delta perspective
11:53
just understand that you don't even in
11:56
the most common way is to have a time
11:59
and then subtract or add that from a
12:01
moment you can actually also
12:05
subtract and add time deltas from time
12:08
delta so we'll do days delta
12:14
and you'll see that now we have the sum
12:17
of those and then we could also then go
12:20
in you know the past so to speak right
12:22
so just be aware that even though the
12:24
most common method uh is to subtract the
12:29
uh you know date time you can also
12:33
deltas right time deltas from one
12:36
etc so now that we understand how to
12:41
work with time deltas they're pretty
12:43
easy let's figure out and understand
12:46
how we can access the date time
12:49
attributes but now let's take a look at
12:51
some of the attributes that we can
12:53
potentially use and they'll explain why
12:55
we use them and give you some concrete
12:57
examples but first let's create the
12:59
heading so accepting attributes
13:04
okay now you can go to uh the
13:06
documentation and you can see the class
13:09
attributes or the instant instance
13:12
attributes right here
13:16
use dir so we'll do bir on now
13:20
and you can see all of the uh you know
13:24
you know class attributes and instant
13:27
attributes and everything right here
13:29
so let's just think about what we want
13:32
now let's just say we want to grab the
13:35
year and this is a common pattern
13:38
whenever let's say you want to download
13:40
a bunch of stock data well you might
13:42
want to separate the data into months or
13:46
that's something that i did where you
13:47
had to use year month you know etc and
13:51
um you know there's also these uh class
13:54
attributes for minimum
13:56
and maximum which are the minimum and
13:58
maximum dates that you can have so if
14:00
you want to download all of the stock
14:02
data and you don't give a specific date
14:05
but that you know maybe that's required
14:07
you can use a minimum and maximum to get
14:10
all you know the dates between you know
14:12
two new points um granted you probably
14:15
don't would have to loop through all
14:16
that but just to give you an example the
14:19
same thing is also true for time deltas
14:22
we can just do all delta
14:30
but if we're curious on where to get
14:32
those you can either go to the
14:33
documentation or just do dir all delta
14:38
and you'll see the same thing with all
14:40
the various methods attributes etc
14:43
so accessing those attributes are pretty
14:47
for various programming tasks such as
14:50
you know the aforementioned you know
14:51
splitting the data up into monthly
14:56
now we're in the home stretch we'll
14:58
create a new cell for adding time zones
15:02
and time zones are pretty conceptually
15:04
easy to understand and they're you know
15:06
all of this stuff is i would say it's
15:08
easier you just have to do it a few
15:10
times to get comfortable with it so we
15:12
haven't been using time zones so far we
15:15
can see that by saying now dot easy info
15:18
equals none which is true because no
15:20
time zone info has been set
15:23
but if we want to set a time zone we can
15:25
do it multiple ways we can actually say
15:30
date time now and then pass it a time
15:37
and then we can print now
15:40
and if you're working with stock market
15:41
data it's almost always advisable to use
15:48
sometimes you have to work with various
15:51
time zones or different programming
15:54
you know tasks require time zone usage
15:57
and you have to set that time zone so
15:58
anyways point is simple to add vtc but
16:02
as we know we can also create date times
16:07
date time equals date time date time
16:13
we'll do let's see uh 9 36 1 2 3 4 5 6
16:18
and this time you know we want to add
16:20
easy info and pi t z qtc
16:33
at utc and see what happens there
16:36
perfect okay so we can see that um
16:40
we now have you know no offset here
16:43
right but we do have the time zone data
16:46
available and if you actually just
16:51
bdtc you could actually see the tz info
16:54
is now added to this uh daytime object
16:58
now one thing is you're saying well this
17:00
is great leo but i'm in the eastern time
17:02
zone and i want to add this to my
17:04
time that's pretty easy to do there's a
17:06
multiple ways you could do it you could
17:08
add it up here you could add it here or
17:10
you can actually replace and that's what
17:12
we'll do now so we'll do
17:14
now right that'll be better right right
17:16
now so we have the now
17:25
but this time what we're going to do
17:28
is we're going to replace
17:31
and we'll do tv info equals pi
17:34
easy time zone and this time we can add
17:37
a time zone we'll do america
17:42
this is my time zone and then we'll
17:48
okay and now we can see that's a lot of
17:51
that the time zone hasn't been changed
17:54
before it was 22 44 right and now we can
18:03
so that's uh that's interesting so you
18:05
might say leo how do i you know that's
18:07
great but how do i find my time zone how
18:10
do i get a list of time zones well you
18:12
can either type in pi td and type in
18:17
for the most commonly used time zones or
18:21
you have a location that's not in there
18:23
you can do all time zones and that will
18:25
give you every single time zone and
18:30
see that wasn't so bad hopefully i
18:32
accomplished my goal of helping you get
18:34
that much further in your python
18:36
programming journey if i did please give
18:38
this video a like and maybe even
18:40
subscribe to the channel it lets the
18:42
google algorithm know that this is a
18:43
video we're sharing and if you're
18:45
interested in time series analysis which
18:49
a probable next step you can go ahead
18:51
and check out this series here where i
18:53
cover pretty much everything you need to
18:54
go from start to finish regarding
18:57
understanding and analyzing a time
18:59
series so hopefully you enjoyed and
19:02
maybe i'll see you next one thanks bye