WEBVTT

00:00.700 --> 00:06.340
In this video we will learn how to use the shift method and actually this method is a very powerful

00:06.340 --> 00:13.240
method that allows us to calculate the absolute change also the relative change in stock price from

00:13.240 --> 00:18.160
yesterday to today or from last week to today or whatever.

00:18.160 --> 00:19.590
So we are still important.

00:19.600 --> 00:21.940
Our close data frame.

00:22.150 --> 00:28.480
So here we have the closing prices for the six stocks and in this video we want to only work with the

00:28.480 --> 00:29.620
Apple stock.

00:29.620 --> 00:31.420
So we selected the apple column.

00:31.420 --> 00:38.350
We create a copier and we transform the panel serious into a data frame and then we store the new data

00:38.350 --> 00:41.520
frame the apple data frame in the available Apple.

00:42.970 --> 00:45.160
And let's have a look here.

00:45.190 --> 00:52.520
So this is our apple data frame with daily stock prices from 2009 until 2019.

00:52.630 --> 00:58.660
And let's assume that for each day we want to calculate the price increase or decrease from the last

00:58.660 --> 00:59.890
trading day.

00:59.890 --> 01:07.820
So for example on the fourth of January the increase from the last trading day was about or point four

01:07.870 --> 01:13.600
dollars and instead of calculating the absolute difference we could also calculate here the relative

01:13.600 --> 01:19.390
difference or the percentage change and therefore on very fast that we want to shift down all values

01:19.390 --> 01:23.170
here in the applet column by actually 1 timestamp.

01:23.440 --> 01:29.740
So we want to have here the price at the thirty first of December we want to have this now at the 4th

01:29.740 --> 01:30.420
of January.

01:30.430 --> 01:36.580
So we want to shift that by one timestamp and we can do this with the shift method.

01:36.580 --> 01:38.950
So let's have a look here.

01:39.010 --> 01:45.970
So the shift method actually shifts the index or we could also say the value is so it's relatively they

01:45.970 --> 01:47.020
are shifted.

01:47.020 --> 01:53.890
So the shift method shifts the index by a desired number of periods and we have fear the pyramid up

01:53.970 --> 01:56.610
periods and by default it set to 1.

01:56.650 --> 02:00.870
But we can also set here different numbers of course.

02:00.880 --> 02:09.420
So now let's try here a shift of 1 so he we can see at the very first timestamp that we have now our

02:09.410 --> 02:12.750
missing value because there is no prior value here.

02:12.860 --> 02:20.530
Then on the fourth of January we have the former value of the thirty first of December so thirty point

02:20.570 --> 02:21.270
one zero.

02:21.290 --> 02:25.160
And so on and of course we can also shift by more than one period.

02:25.180 --> 02:28.290
So let's pass here too.

02:28.550 --> 02:34.040
So now we can see here that we have here two missing values and the very first value we have yet the

02:34.040 --> 02:41.510
first of January and this is the value from the thirty first of December which is actually two business

02:41.510 --> 02:44.150
days or trading days here of a.

02:44.150 --> 02:50.740
And instead of passing a positive number so 1 or 2 you can also pass a negative in the test.

02:50.780 --> 02:53.590
So for example minus one.

02:53.870 --> 02:59.180
And by doing so we are actually a relatively a shifting up here the values in the column.

02:59.210 --> 03:05.480
So now here for example for the thirty first of December we have the value that was formerly here at

03:05.900 --> 03:07.740
the 4th of January.

03:08.060 --> 03:13.620
So for each timestamp here and our shift that data frame we have an hour the value of the next stop

03:13.620 --> 03:20.420
for the following business day and in some cases that can make sense but usually we are shifting in

03:20.570 --> 03:22.070
the other direction here.

03:26.570 --> 03:33.400
So for each timestamp we fear the values from the past and we can compare current values with past values.

03:33.400 --> 03:37.250
So this is a frequent workflow and Finance for example.

03:37.640 --> 03:45.050
And once we are happy with our shift then we can also create a new column and save the sifted values.

03:45.050 --> 03:48.410
So for example we can call you at the column like one.

03:48.410 --> 03:55.960
So we have a lack of one period here and lets the re inspect our data frame.

03:56.100 --> 04:02.270
So are on the right hand side we have now our like one and now we can actually calculate the daily increase

04:02.270 --> 04:10.940
or decrease in stock prices and we can do this by selecting the upper column and subtract actually our

04:10.940 --> 04:14.780
like one column and we can do this here with the sub method.

04:14.810 --> 04:15.890
So let's do this year

04:19.240 --> 04:25.030
and see we can see for example that the EPA's stock price increased from the study first of December

04:25.030 --> 04:30.220
to the fourth of an area by or point forty six dollars.

04:30.340 --> 04:33.760
So this is exactly here this difference.

04:33.790 --> 04:35.020
All right.

04:35.170 --> 04:40.630
Now we can also create a new column called the Friends where we actually store here the pandas series

04:40.840 --> 04:42.340
that we created here.

04:42.700 --> 04:47.890
So it's the daily increase or decrease in stock price.

04:47.890 --> 04:50.030
So now we have you on the right hand side.

04:50.050 --> 04:52.720
The difference between those two columns.

04:53.410 --> 04:59.260
So here we have now the absolute difference but in finance typically it's more meaningful to have a

04:59.260 --> 05:04.030
relative increase or decrease or a percentage change are in finance terms.

05:04.030 --> 05:12.010
Also call it return and we can calculate the percentage change or return by selecting the above column

05:12.430 --> 05:19.580
and then we divide here the values in the upper column by the values in the like one column.

05:19.690 --> 05:21.550
So let's do this here

05:24.850 --> 05:30.880
and for example here we can see that from the thirty first of December to the 4th of January the stock

05:30.940 --> 05:34.170
increased by one point fifty five percent.

05:34.420 --> 05:40.390
So if you want to get now the percentage change or the stock return in percentage terms to be of all

05:40.390 --> 05:43.830
have to subtract one here so we can do this.

05:43.830 --> 05:49.750
This the sub method and then we have to multiply its value by 100

05:52.340 --> 05:56.930
so give us now the daily percentage changes our returns.

05:56.930 --> 06:02.140
So for example for the Fourth of 10 year weaken CBS a return of one point five five percent.

06:02.870 --> 06:10.420
And for example on the 6th of January we have a negative return off from minus the one point five nine

06:10.430 --> 06:11.650
percent.

06:11.660 --> 06:19.640
All right and finally we can also create a new column percentage change where we actually pass uh the

06:19.640 --> 06:21.380
various independent serious

06:24.080 --> 06:27.170
and now we can finally inspect our data frame.

06:27.710 --> 06:34.670
So here in this video we created a like one column where we actually sift at the daily prices by one

06:34.760 --> 06:42.110
timestamp and by doing so we could calculate the daily increase or decrease in stock prices and we could

06:42.110 --> 06:45.790
also calculate the daily percentage change return.

06:45.980 --> 06:51.130
And with this we are finished with this video and I hope to see you also in the next one by.
