WEBVTT

00:00.760 --> 00:07.330
In the last video we imported our very first time series data and we converted the column daytime into

00:07.330 --> 00:10.090
the daytime index of our data frame.

00:10.240 --> 00:16.970
And we did this by passing the column labor to the pyramids past states and index call of the Read CSP

00:17.020 --> 00:18.000
method.

00:18.160 --> 00:23.680
And in this video we will have a look at the second more explicit alternative to do this and two we

00:23.680 --> 00:24.460
need two steps.

00:24.460 --> 00:29.920
So first of all we transform the data type of the column to date time and we can do this.

00:29.920 --> 00:34.310
This is the direct pen us method to date time and the second.

00:34.330 --> 00:41.320
We said the column is the index of our data frame this the asset index method and then in the second

00:41.320 --> 00:48.160
part of this video we will have a closer look to the to date time method and we plan in more detail

00:48.160 --> 00:52.070
how to transform different strings into daytime objects.

00:52.270 --> 01:02.470
So let's import pandas here and we import our temperate CSP file and TV for the first five rows and

01:02.470 --> 01:04.920
we also quality of the Inform method.

01:04.930 --> 01:09.040
So here we have now the column day time and we offer the data type object.

01:09.040 --> 01:15.100
So these are strings here and we can also explicitly select the date time column.

01:15.100 --> 01:16.020
So let's do this

01:18.740 --> 01:26.090
and we can see at the end of that year we have the data type object and we can also select a single

01:26.090 --> 01:30.810
element of our panda series or column slow.

01:30.820 --> 01:36.950
Here we can see that this is actually a string and now we can convert the data type of our column to

01:36.950 --> 01:41.440
date time with the direct pen us method to date time.

01:41.450 --> 01:44.420
So here we pass our column date time

01:47.350 --> 01:51.850
and also you will get a pen that serious with all of our dates here.

01:51.990 --> 01:57.430
And if you have a look here at the very end we can see here that we have to date that type of daytime

01:57.430 --> 02:00.170
60 for service of the method daytime.

02:00.230 --> 02:05.970
We can transform strings into a daytime objects and now we're going to want to have the daytime column

02:05.970 --> 02:12.900
here as being the daytime index of our whole data frame and we can do this in two steps.

02:12.900 --> 02:18.570
So first of all we convert the data type with the method to data time and by doing so we're getting

02:18.570 --> 02:25.280
you actually hear the pandas series and then we pass here the pandas serious to the method the set index.

02:25.350 --> 02:32.800
So let's try this out here so we can see them now on the left hand side that instead of having a range

02:32.830 --> 02:40.300
index we have our column date time and still we have the column day time so we can drop the column here

02:43.000 --> 02:44.830
and now this looks quite good.

02:44.830 --> 02:49.380
And uh then we can override our variable temperature so let's do this

02:52.840 --> 02:59.340
and that's again never look at the first five rows so here we have our new data frame we have on the

02:59.340 --> 03:05.160
left hand side our daytime index and we have the two columns L.A. and New York.

03:05.160 --> 03:12.330
And let's also inspect the info method so we can see now our daytime indexes are worth thirty five thousand

03:12.330 --> 03:17.850
entries and our two columns and we can also further inspect our daytime index.

03:17.870 --> 03:19.600
But the attribute index.

03:19.620 --> 03:25.680
So here we have the daytime index with all of the time stems and we can also select a single element

03:25.680 --> 03:26.410
here.

03:26.430 --> 03:33.210
So the first one and here we have a timestamp and actually panned US is quite smart and versatile when

03:33.210 --> 03:36.260
it comes to converting strings to daytime optics.

03:36.390 --> 03:39.350
So actually panels us can handle many different formats.

03:39.390 --> 03:45.000
And uh the most send out format is actually a 215 or 520.

03:45.030 --> 03:50.990
So we have here the 20th of May 2015 and we have here the format year.

03:51.000 --> 03:52.710
Then we have a minus or dash.

03:52.710 --> 03:57.250
Then we have months then and then minus or dash and then we have the day.

03:57.300 --> 04:01.230
And this is no problem to convert into a daytime object.

04:01.350 --> 04:03.090
So here we get the timestamp.

04:03.390 --> 04:09.360
So you know our string we have the information about the date but uh we haven't passed any information

04:09.360 --> 04:10.390
on the time.

04:10.440 --> 04:13.470
And by default US assumes actually at midnight.

04:13.490 --> 04:16.400
So we have uh Midnight Zero clock actually.

04:16.420 --> 04:23.190
So this is here for the hours the minutes and the seconds but within I was doing we can also pass information

04:23.190 --> 04:24.530
on the time.

04:24.600 --> 04:32.820
So let's do this for example 10:00 and then we get to your 10:00 and we can also provide information

04:32.820 --> 04:33.690
on the minutes.

04:33.690 --> 04:36.450
So let's assume 10 30.

04:38.280 --> 04:41.910
And we can also provide additional information on these seconds.

04:41.910 --> 04:44.470
So for example 20 seconds.

04:44.610 --> 04:51.300
And as I said before I actually PANDAS and the method to date time is quite powerful and versatile because

04:51.300 --> 04:55.230
it can actually recognize and convert many different formats.

04:55.230 --> 04:57.770
So for example here we have a different format.

04:57.810 --> 05:02.480
So we have between year month and day no separate at all.

05:02.510 --> 05:04.260
But this still works.

05:04.260 --> 05:06.630
And again here by default we have at midnight.

05:06.690 --> 05:12.480
If we do not pass any information on the time and we can also use different separate us.

05:12.480 --> 05:14.670
So for example here slashes

05:17.420 --> 05:24.620
or we can use that byte spaces and that's only one thing that we have to keep in mind here.

05:24.630 --> 05:29.940
So by default the sequence this year month and day.

05:30.300 --> 05:33.780
And for example if you pass now a year day and month.

05:33.780 --> 05:39.460
So our intention here is to get the data the 20th of May 2015.

05:39.480 --> 05:44.870
But here we switched the day and month and here penned us gets the problem.

05:44.880 --> 05:51.310
So here we get an error message and it says months must be between one and twelve.

05:51.360 --> 05:58.710
So by default pen US expects here the sequence that year month and day and this is definitely a source

05:58.710 --> 06:04.860
for errors but we can also prevent this by having for the month for example in May.

06:05.430 --> 06:14.160
So this also works and we can also locate the May at the very left side of our string or on the right

06:14.160 --> 06:15.800
hand side.

06:15.990 --> 06:21.390
And actually it doesn't matter if we have here uppercase let us all lowercase letters or whatever.

06:21.480 --> 06:22.540
So it works.

06:22.680 --> 06:27.870
And instead of having 20 for the day become can also for your 20th with the T H.

06:27.870 --> 06:29.560
So this also works.

06:29.580 --> 06:33.000
So to summarize so there's hardly any combination here.

06:33.020 --> 06:40.650
It does not work and we can also pass a sequence with dates to the to date time method.

06:40.650 --> 06:47.530
So for example here we have a list and here above we passed a panda series.

06:48.440 --> 06:50.310
Our column dates time

06:53.070 --> 06:56.180
and now we simply pass a list with two dates.

06:56.220 --> 07:03.960
And this also works here and whenever we pass more than one date we get here a date time index.

07:03.990 --> 07:12.030
So here you have now a day time index with two time stamps and we can also pass a sequence of dates

07:12.180 --> 07:14.220
where for at least one element.

07:14.490 --> 07:19.590
Even pandas will not be able to convert this into daytime so for example elephant.

07:19.800 --> 07:26.880
So this might be impossible to convert but we can try here and let's have a look what daytime does here

07:27.540 --> 07:34.620
and by default we get here an error message so unknown string format elephant.

07:34.620 --> 07:38.170
So we have senior buffer that pandas can handle many string formats.

07:38.190 --> 07:42.060
But of course elephant is quite impossible to convert.

07:42.240 --> 07:44.190
And by default we are getting an error message.

07:44.190 --> 07:46.050
But we can change this.

07:46.050 --> 07:52.090
So let's have a look here and here we have the parameter arrows.

07:52.370 --> 07:54.360
And by default it's set to raise.

07:54.410 --> 08:03.220
So let's have a look here at the documentation so if we have the argument raise then the invalid passing

08:03.220 --> 08:04.420
that raise an exception.

08:04.420 --> 08:10.150
So we're getting an error message but we can also possibly coerce and then the invalid passing will

08:10.150 --> 08:12.010
be set as an 80 year.

08:12.070 --> 08:14.050
So this stands for not a time.

08:14.770 --> 08:18.940
So let's try this out and let's pass codes to the parameter arrows

08:23.370 --> 08:30.240
and it's from the cell here and here we can see that we nevertheless get a daytime index with the two

08:30.240 --> 08:37.410
time stamps and actually the element an 80 year which stands for not a time.

08:37.410 --> 08:44.490
So also here and having some strings which cannot be converted to daytime then still the daytime method

08:44.550 --> 08:46.620
is quite smart and versatile.

08:47.340 --> 08:47.910
All right.

08:47.940 --> 08:52.590
With this we are finished now with this video and I hope to see you also in the next one by.
