Getting Started with Python¶

Working with Time Series Data¶

Author : Waweru Kennedy¶

Date: 16/3/2022¶

Resources¶

In [2]:
import os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import datetime
In [2]:
os.listdir()
Out[2]:
['.ipynb_checkpoints', 'new_daily_prices.csv', 'Untitled.ipynb']
In [3]:
df = pd.read_csv('new_daily_prices.csv')
df.head()
Out[3]:
Date Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
0 Thursday, 13 May 2021 15.0 360.00 340.0 18.80 146.75 20.5 8.80 26.0 65.00 ... 471.00 10.90 172.25 1.09 1.41 10.4 0.27 31.8 39.40 6.10
1 Friday, 21 May 2021 15.0 360.00 340.0 18.55 145.00 22.0 9.10 26.0 63.75 ... 470.00 10.40 174.00 1.04 1.35 10.4 0.27 28.0 39.50 5.98
2 Tuesday, 25 May 2021 15.0 360.00 340.0 18.40 145.00 24.2 9.22 26.0 63.25 ... 464.75 10.15 181.00 0.99 1.40 10.4 0.27 28.0 42.75 5.96
3 Monday, 19 July 2021 11.0 372.75 290.0 19.20 152.25 31.0 10.00 28.0 69.00 ... 445.00 12.00 187.75 1.04 1.32 10.4 0.27 27.1 42.60 6.66
4 Tuesday, 27 July 2021 11.5 410.00 280.0 19.95 134.00 35.2 9.74 29.0 66.25 ... 469.50 12.05 184.00 1.01 1.33 10.4 0.27 29.0 41.80 6.94

5 rows × 61 columns

In [4]:
df.tail()
Out[4]:
Date Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
102 Wednesday, 05 January 2022 12.85 420.0 320.0 19.95 130.75 30.60 11.75 29.50 59.75 ... 442.0 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02
103 Thursday, 06 January 2022 13.80 420.0 320.0 20.25 130.75 33.70 11.80 29.30 60.00 ... 442.0 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
104 Friday, 07 January 2022 13.80 420.0 320.0 21.25 132.00 37.05 11.80 29.05 60.00 ... 442.0 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
105 Tuesday, 11 January 2022 13.80 385.0 320.0 20.55 134.75 44.75 11.90 30.75 59.50 ... 445.0 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
106 Thursday, 13 January 2022 12.90 385.0 320.0 22.20 130.00 54.00 11.80 30.00 59.00 ... 440.0 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52

5 rows × 61 columns

In [5]:
df = pd.read_csv('new_daily_prices.csv',index_col=0)
In [6]:
df.head()
Out[6]:
Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 Equity Group Holdings Ord 0.50 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
Date
Thursday, 13 May 2021 15.0 360.00 340.0 18.80 146.75 20.5 8.80 26.0 65.00 40.95 ... 471.00 10.90 172.25 1.09 1.41 10.4 0.27 31.8 39.40 6.10
Friday, 21 May 2021 15.0 360.00 340.0 18.55 145.00 22.0 9.10 26.0 63.75 41.50 ... 470.00 10.40 174.00 1.04 1.35 10.4 0.27 28.0 39.50 5.98
Tuesday, 25 May 2021 15.0 360.00 340.0 18.40 145.00 24.2 9.22 26.0 63.25 42.05 ... 464.75 10.15 181.00 0.99 1.40 10.4 0.27 28.0 42.75 5.96
Monday, 19 July 2021 11.0 372.75 290.0 19.20 152.25 31.0 10.00 28.0 69.00 48.55 ... 445.00 12.00 187.75 1.04 1.32 10.4 0.27 27.1 42.60 6.66
Tuesday, 27 July 2021 11.5 410.00 280.0 19.95 134.00 35.2 9.74 29.0 66.25 48.50 ... 469.50 12.05 184.00 1.01 1.33 10.4 0.27 29.0 41.80 6.94

5 rows × 60 columns

In [7]:
df.tail()
Out[7]:
Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 Equity Group Holdings Ord 0.50 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
Date
Wednesday, 05 January 2022 12.85 420.0 320.0 19.95 130.75 30.60 11.75 29.50 59.75 53.00 ... 442.0 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02
Thursday, 06 January 2022 13.80 420.0 320.0 20.25 130.75 33.70 11.80 29.30 60.00 53.00 ... 442.0 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
Friday, 07 January 2022 13.80 420.0 320.0 21.25 132.00 37.05 11.80 29.05 60.00 53.00 ... 442.0 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
Tuesday, 11 January 2022 13.80 385.0 320.0 20.55 134.75 44.75 11.90 30.75 59.50 52.00 ... 445.0 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
Thursday, 13 January 2022 12.90 385.0 320.0 22.20 130.00 54.00 11.80 30.00 59.00 49.55 ... 440.0 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52

5 rows × 60 columns

In [8]:
df.shape
Out[8]:
(107, 60)
In [9]:
df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 107 entries,  Thursday, 13 May 2021 to  Thursday, 13 January 2022
Data columns (total 60 columns):
 #   Column                                        Non-Null Count  Dtype  
---  ------                                        --------------  -----  
 0   Eaagads Ltd Ord 1.25 AIMS                     107 non-null    float64
 1   Kakuzi Ord.5.00                               107 non-null    float64
 2   Limuru Tea Co. Ltd Ord 20.00                  107 non-null    float64
 3   Sasini Ltd Ord 1.00                           107 non-null    float64
 4   Williamson Tea Kenya Ltd Ord 5.00             107 non-null    float64
 5   Car and General (K) Ltd Ord 5.00              107 non-null    float64
 6   Absa Bank Kenya PLC                           107 non-null    float64
 7   BK Group PLC                                  107 non-null    float64
 8   Diamond Trust Bank Kenya Ltd Ord 4.00         107 non-null    float64
 9   Equity Group Holdings Ord 0.50                107 non-null    float64
 10  HF Group Ltd Ord 5.00                         107 non-null    float64
 11  I&M Holdings Ltd Ord 1.00                     107 non-null    float64
 12  KCB Group Ltd Ord 1.00                        107 non-null    float64
 13  National Bank of Kenya Ltd Ord 5.00           107 non-null    float64
 14  NCBA Group PLC                                107 non-null    float64
 15   Stanbic Holdings Plc. ord.5.00               107 non-null    float64
 16  Standard Chartered Bank Ltd Ord 5.00          107 non-null    float64
 17  The Co-operative Bank of Kenya Ltd Ord 1.00   107 non-null    float64
 18  Express Ltd Ord 5.00                          107 non-null    float64
 19  Kenya Airways Ltd Ord 5.00                    107 non-null    float64
 20  Longhorn Publishers Ltd                       107 non-null    float64
 21   Nairobi Business Ventures Ltd                107 non-null    float64
 22  Nation Media Group Ord. 2.50                  107 non-null    float64
 23  Sameer Africa PLC Ord 5.00                    107 non-null    float64
 24  Scangroup  Ltd Ord 1.00                       107 non-null    float64
 25  Standard Group  Ltd Ord 5.00                  107 non-null    float64
 26  TPS Eastern Africa (Serena) Ltd Ord 1.00      107 non-null    float64
 27  Uchumi Supermarket Ltd Ord 5.00               107 non-null    float64
 28  Athi River Mining Ord 5.00                    107 non-null    float64
 29  Bamburi Cement Ltd Ord 5.00                   107 non-null    float64
 30  Crown Paints Kenya PLC. 0rd 5.00              107 non-null    float64
 31  E.A.Cables Ltd Ord 0.50                       107 non-null    float64
 32  E.A.Portland Cement Ltd Ord 5.00              107 non-null    float64
 33  KenGen Ltd  Ord. 2.50                         107 non-null    float64
 34   Kenya Power & Lighting  Co Ltd               107 non-null    float64
 35  Total Kenya Ltd Ord 5.00                      107 non-null    float64
 36  Umeme Ltd Ord 0.50                            107 non-null    float64
 37  Britam Holdings Ltd Ord 0.10                  107 non-null    float64
 38  CIC Insurance Group Ltd Ord 1.00              107 non-null    float64
 39  Jubilee Holdings Ltd Ord 5.00                 107 non-null    float64
 40  Kenya Re-Insurance Corporation Ltd Ord 2.50   107 non-null    float64
 41  Liberty Kenya Holdings Ltd                    107 non-null    float64
 42  Sanlam Kenya PLC 0rd 5.00                     107 non-null    float64
 43  Centum Investment Co Ltd Ord 0.50             107 non-null    float64
 44   Home Afrika Ltd Ord 1.00                     107 non-null    float64
 45  Kurwitu Ventures                              107 non-null    object 
 46  Olympia Capital Holdings ltd Ord 5.00         107 non-null    float64
 47  Trans-Century Ltd                             107 non-null    float64
 48  Nairobi Securities Exchange Ltd Ord 4.00      107 non-null    float64
 49  B.O.C Kenya Ltd Ord 5.00                      107 non-null    float64
 50  British American Tobacco Kenya Ltd Ord 10.00  107 non-null    float64
 51  Carbacid Investments Ltd Ord 5.00             107 non-null    float64
 52  East African Breweries Ltd Ord 2.00           107 non-null    float64
 53  Eveready East Africa Ltd Ord.1.00             107 non-null    float64
 54  Flame Tree Group Holdings Ltd Ord 0.825       107 non-null    float64
 55  Kenya Orchards Ltd Ord 5.00                   107 non-null    float64
 56  Mumias Sugar Co. Ltd Ord 2.00                 107 non-null    float64
 57  Unga Group Ltd Ord 5.00                       107 non-null    float64
 58  Safaricom PLC Ord 0.05                        107 non-null    float64
 59  Stanlib Fahari I-REIT                         107 non-null    float64
dtypes: float64(59), object(1)
memory usage: 51.0+ KB
In [10]:
df['Kurwitu Ventures'].apply(lambda x: x.replace(",","").split(".")[0])
Out[10]:
Date
 Thursday, 13 May 2021         1500
 Friday, 21 May 2021           1500
 Tuesday, 25 May 2021          1500
 Monday, 19 July 2021          1500
 Tuesday, 27 July 2021         1500
                               ... 
 Wednesday, 05 January 2022    1500
 Thursday, 06 January 2022     1500
 Friday, 07 January 2022       1500
 Tuesday, 11 January 2022      1500
 Thursday, 13 January 2022     1500
Name: Kurwitu Ventures, Length: 107, dtype: object
In [11]:
df['Kurwitu Ventures'] = df['Kurwitu Ventures'].str.replace(",","")
In [17]:
df['Kurwitu Ventures'] = df['Kurwitu Ventures'].apply(lambda x: x.split(".")[0])
df['Kurwitu Ventures']
Out[17]:
Date
 Thursday, 13 May 2021         1500
 Friday, 21 May 2021           1500
 Tuesday, 25 May 2021          1500
 Monday, 19 July 2021          1500
 Tuesday, 27 July 2021         1500
                               ... 
 Wednesday, 05 January 2022    1500
 Thursday, 06 January 2022     1500
 Friday, 07 January 2022       1500
 Tuesday, 11 January 2022      1500
 Thursday, 13 January 2022     1500
Name: Kurwitu Ventures, Length: 107, dtype: object
In [18]:
df['Kurwitu Ventures'].astype(int)
Out[18]:
Date
 Thursday, 13 May 2021         1500
 Friday, 21 May 2021           1500
 Tuesday, 25 May 2021          1500
 Monday, 19 July 2021          1500
 Tuesday, 27 July 2021         1500
                               ... 
 Wednesday, 05 January 2022    1500
 Thursday, 06 January 2022     1500
 Friday, 07 January 2022       1500
 Tuesday, 11 January 2022      1500
 Thursday, 13 January 2022     1500
Name: Kurwitu Ventures, Length: 107, dtype: int32
In [20]:
df['Kurwitu Ventures'] = df['Kurwitu Ventures'].astype(int)
df.dtypes
Out[20]:
Eaagads Ltd Ord 1.25 AIMS                       float64
Kakuzi Ord.5.00                                 float64
Limuru Tea Co. Ltd Ord 20.00                    float64
Sasini Ltd Ord 1.00                             float64
Williamson Tea Kenya Ltd Ord 5.00               float64
Car and General (K) Ltd Ord 5.00                float64
Absa Bank Kenya PLC                             float64
BK Group PLC                                    float64
Diamond Trust Bank Kenya Ltd Ord 4.00           float64
Equity Group Holdings Ord 0.50                  float64
HF Group Ltd Ord 5.00                           float64
I&M Holdings Ltd Ord 1.00                       float64
KCB Group Ltd Ord 1.00                          float64
National Bank of Kenya Ltd Ord 5.00             float64
NCBA Group PLC                                  float64
 Stanbic Holdings Plc. ord.5.00                 float64
Standard Chartered Bank Ltd Ord 5.00            float64
The Co-operative Bank of Kenya Ltd Ord 1.00     float64
Express Ltd Ord 5.00                            float64
Kenya Airways Ltd Ord 5.00                      float64
Longhorn Publishers Ltd                         float64
 Nairobi Business Ventures Ltd                  float64
Nation Media Group Ord. 2.50                    float64
Sameer Africa PLC Ord 5.00                      float64
Scangroup  Ltd Ord 1.00                         float64
Standard Group  Ltd Ord 5.00                    float64
TPS Eastern Africa (Serena) Ltd Ord 1.00        float64
Uchumi Supermarket Ltd Ord 5.00                 float64
Athi River Mining Ord 5.00                      float64
Bamburi Cement Ltd Ord 5.00                     float64
Crown Paints Kenya PLC. 0rd 5.00                float64
E.A.Cables Ltd Ord 0.50                         float64
E.A.Portland Cement Ltd Ord 5.00                float64
KenGen Ltd  Ord. 2.50                           float64
 Kenya Power & Lighting  Co Ltd                 float64
Total Kenya Ltd Ord 5.00                        float64
Umeme Ltd Ord 0.50                              float64
Britam Holdings Ltd Ord 0.10                    float64
CIC Insurance Group Ltd Ord 1.00                float64
Jubilee Holdings Ltd Ord 5.00                   float64
Kenya Re-Insurance Corporation Ltd Ord 2.50     float64
Liberty Kenya Holdings Ltd                      float64
Sanlam Kenya PLC 0rd 5.00                       float64
Centum Investment Co Ltd Ord 0.50               float64
 Home Afrika Ltd Ord 1.00                       float64
Kurwitu Ventures                                  int32
Olympia Capital Holdings ltd Ord 5.00           float64
Trans-Century Ltd                               float64
Nairobi Securities Exchange Ltd Ord 4.00        float64
B.O.C Kenya Ltd Ord 5.00                        float64
British American Tobacco Kenya Ltd Ord 10.00    float64
Carbacid Investments Ltd Ord 5.00               float64
East African Breweries Ltd Ord 2.00             float64
Eveready East Africa Ltd Ord.1.00               float64
Flame Tree Group Holdings Ltd Ord 0.825         float64
Kenya Orchards Ltd Ord 5.00                     float64
Mumias Sugar Co. Ltd Ord 2.00                   float64
Unga Group Ltd Ord 5.00                         float64
Safaricom PLC Ord 0.05                          float64
Stanlib Fahari I-REIT                           float64
dtype: object
In [21]:
df['Safaricom PLC Ord 0.05']
Out[21]:
Date
 Thursday, 13 May 2021         39.40
 Friday, 21 May 2021           39.50
 Tuesday, 25 May 2021          42.75
 Monday, 19 July 2021          42.60
 Tuesday, 27 July 2021         41.80
                               ...  
 Wednesday, 05 January 2022    39.55
 Thursday, 06 January 2022     40.00
 Friday, 07 January 2022       39.90
 Tuesday, 11 January 2022      38.45
 Thursday, 13 January 2022     37.95
Name: Safaricom PLC Ord 0.05, Length: 107, dtype: float64
In [22]:
df['Safaricom PLC Ord 0.05'].mean()
Out[22]:
40.94392523364486

The time intervals applied to assemble the collected data in a chronological order are called the time series frequency.

In [23]:
# convert to time series format (latest date first)
df['Safaricom PLC Ord 0.05'][::-1]
Out[23]:
Date
 Thursday, 13 January 2022     37.95
 Tuesday, 11 January 2022      38.45
 Friday, 07 January 2022       39.90
 Thursday, 06 January 2022     40.00
 Wednesday, 05 January 2022    39.55
                               ...  
 Tuesday, 27 July 2021         41.80
 Monday, 19 July 2021          42.60
 Tuesday, 25 May 2021          42.75
 Friday, 21 May 2021           39.50
 Thursday, 13 May 2021         39.40
Name: Safaricom PLC Ord 0.05, Length: 107, dtype: float64
In [24]:
## We can reverse the whole DataFrame the same way

df[::-1]
Out[24]:
Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 Equity Group Holdings Ord 0.50 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
Date
Thursday, 13 January 2022 12.90 385.00 320.0 22.20 130.00 54.00 11.80 30.00 59.00 49.55 ... 440.00 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52
Tuesday, 11 January 2022 13.80 385.00 320.0 20.55 134.75 44.75 11.90 30.75 59.50 52.00 ... 445.00 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
Friday, 07 January 2022 13.80 420.00 320.0 21.25 132.00 37.05 11.80 29.05 60.00 53.00 ... 442.00 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
Thursday, 06 January 2022 13.80 420.00 320.0 20.25 130.75 33.70 11.80 29.30 60.00 53.00 ... 442.00 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
Wednesday, 05 January 2022 12.85 420.00 320.0 19.95 130.75 30.60 11.75 29.50 59.75 53.00 ... 442.00 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Tuesday, 27 July 2021 11.50 410.00 280.0 19.95 134.00 35.20 9.74 29.00 66.25 48.50 ... 469.50 12.05 184.00 1.01 1.33 10.4 0.27 29.00 41.80 6.94
Monday, 19 July 2021 11.00 372.75 290.0 19.20 152.25 31.00 10.00 28.00 69.00 48.55 ... 445.00 12.00 187.75 1.04 1.32 10.4 0.27 27.10 42.60 6.66
Tuesday, 25 May 2021 15.00 360.00 340.0 18.40 145.00 24.20 9.22 26.00 63.25 42.05 ... 464.75 10.15 181.00 0.99 1.40 10.4 0.27 28.00 42.75 5.96
Friday, 21 May 2021 15.00 360.00 340.0 18.55 145.00 22.00 9.10 26.00 63.75 41.50 ... 470.00 10.40 174.00 1.04 1.35 10.4 0.27 28.00 39.50 5.98
Thursday, 13 May 2021 15.00 360.00 340.0 18.80 146.75 20.50 8.80 26.00 65.00 40.95 ... 471.00 10.90 172.25 1.09 1.41 10.4 0.27 31.80 39.40 6.10

107 rows × 60 columns

In [25]:
df = df[::-1]
df1 = df[::-1].copy()
In [26]:
df.head(20)
Out[26]:
Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 Equity Group Holdings Ord 0.50 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
Date
Thursday, 13 January 2022 12.90 385.0 320.0 22.20 130.00 54.00 11.80 30.00 59.00 49.55 ... 440.0 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52
Tuesday, 11 January 2022 13.80 385.0 320.0 20.55 134.75 44.75 11.90 30.75 59.50 52.00 ... 445.0 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
Friday, 07 January 2022 13.80 420.0 320.0 21.25 132.00 37.05 11.80 29.05 60.00 53.00 ... 442.0 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
Thursday, 06 January 2022 13.80 420.0 320.0 20.25 130.75 33.70 11.80 29.30 60.00 53.00 ... 442.0 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
Wednesday, 05 January 2022 12.85 420.0 320.0 19.95 130.75 30.60 11.75 29.50 59.75 53.00 ... 442.0 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02
Tuesday, 04 January 2022 12.85 385.0 320.0 18.70 132.00 33.95 11.80 27.00 59.75 53.25 ... 440.0 10.80 165.00 1.00 1.25 10.4 0.27 27.65 39.05 6.32
Monday, 03 January 2022 13.50 385.0 320.0 18.70 132.00 33.95 11.75 29.00 59.50 52.75 ... 440.0 10.90 165.50 1.05 1.25 10.4 0.27 27.65 38.15 6.42
Friday, 31 December 2021 13.50 385.0 320.0 18.70 130.00 33.95 11.85 29.00 59.50 52.75 ... 441.5 11.00 165.00 0.98 1.26 10.4 0.27 27.65 37.95 6.26
Thursday, 30 December 2021 13.50 385.0 320.0 18.70 130.00 33.95 11.70 29.00 59.50 51.00 ... 441.5 11.00 165.25 0.98 1.33 10.4 0.27 27.65 37.65 6.26
Wednesday, 29 December 2021 13.50 385.0 320.0 20.75 130.00 31.00 11.80 28.50 60.00 50.00 ... 441.5 10.90 167.75 0.98 1.35 10.4 0.27 27.65 38.75 6.52
Tuesday, 28 December 2021 13.50 385.0 320.0 21.90 130.00 33.95 11.60 28.95 60.00 49.60 ... 435.0 11.00 168.00 0.95 1.35 10.4 0.27 27.65 39.35 6.50
Friday, 24 December 2021 13.80 385.0 320.0 21.90 130.25 33.90 11.55 28.00 59.50 48.50 ... 440.0 11.00 168.00 0.99 1.35 10.4 0.27 27.65 40.00 6.48
Wednesday, 22 December 2021 12.85 385.0 335.0 21.85 138.50 33.90 11.20 28.65 57.25 49.15 ... 436.0 10.65 164.50 1.01 1.35 10.4 0.27 27.65 38.65 6.56
Tuesday, 21 December 2021 12.85 385.0 335.0 21.95 129.00 33.90 11.20 27.40 57.00 49.00 ... 435.0 10.80 161.75 1.04 1.35 10.4 0.27 27.65 38.05 6.60
Monday, 20 December 2021 12.85 385.0 305.0 20.30 138.50 34.00 11.10 26.65 57.25 48.75 ... 435.0 10.80 151.00 1.01 1.35 10.4 0.27 27.65 38.00 6.66
Friday, 17 December 2021 13.60 385.0 300.0 20.30 126.00 34.00 11.20 26.40 57.00 48.65 ... 435.0 10.80 148.25 1.02 1.35 10.4 0.27 27.65 37.95 6.62
Thursday, 16 December 2021 13.60 385.0 300.0 20.55 126.00 34.00 11.00 26.35 57.00 49.35 ... 429.0 10.80 150.00 1.04 1.35 10.4 0.27 27.65 37.75 6.62
Wednesday, 15 December 2021 13.60 385.0 300.0 22.40 126.00 34.00 11.05 26.35 56.00 48.95 ... 429.0 10.70 149.00 1.03 1.35 10.4 0.27 27.65 37.70 6.60
Tuesday, 14 December 2021 13.60 423.5 300.0 20.60 126.00 34.00 11.20 26.35 56.00 48.65 ... 427.5 10.80 149.00 1.04 1.35 10.4 0.27 27.65 37.75 6.62
Friday, 10 December 2021 13.95 423.5 300.0 20.60 126.00 34.00 11.05 26.35 56.00 47.50 ... 430.0 10.80 149.00 1.02 1.35 10.4 0.27 27.65 37.80 6.52

20 rows × 60 columns

In [27]:
df.index
Out[27]:
Index([' Thursday, 13 January 2022', ' Tuesday, 11 January 2022',
       ' Friday, 07 January 2022', ' Thursday, 06 January 2022',
       ' Wednesday, 05 January 2022', ' Tuesday, 04 January 2022',
       ' Monday, 03 January 2022', ' Friday, 31 December 2021',
       ' Thursday, 30 December 2021', ' Wednesday, 29 December 2021',
       ...
       ' Monday, 09 August 2021', ' Friday, 06 August 2021',
       ' Thursday, 05 August 2021', ' Wednesday, 04 August 2021',
       ' Tuesday, 03 August 2021', ' Tuesday, 27 July 2021',
       ' Monday, 19 July 2021', ' Tuesday, 25 May 2021',
       ' Friday, 21 May 2021', ' Thursday, 13 May 2021'],
      dtype='object', name='Date', length=107)
In [ ]:
 
In [31]:
time_str = ' Monday, 09 August 2021' # day of week (%A), day of month (%d) Month Full (%B) YYYY (%Y)
time_str = time_str.strip()
datetime.datetime.strptime(time_str,'%A, %d %B %Y').year
Out[31]:
2021
In [36]:
# convert to pandas date type 
df.index.str.lstrip()
Out[36]:
Index(['Thursday, 13 January 2022', 'Tuesday, 11 January 2022',
       'Friday, 07 January 2022', 'Thursday, 06 January 2022',
       'Wednesday, 05 January 2022', 'Tuesday, 04 January 2022',
       'Monday, 03 January 2022', 'Friday, 31 December 2021',
       'Thursday, 30 December 2021', 'Wednesday, 29 December 2021',
       ...
       'Monday, 09 August 2021', 'Friday, 06 August 2021',
       'Thursday, 05 August 2021', 'Wednesday, 04 August 2021',
       'Tuesday, 03 August 2021', 'Tuesday, 27 July 2021',
       'Monday, 19 July 2021', 'Tuesday, 25 May 2021', 'Friday, 21 May 2021',
       'Thursday, 13 May 2021'],
      dtype='object', name='Date', length=107)
In [37]:
df.index = df.index.str.lstrip()
In [38]:
pd.to_datetime(df.index, format="%A, %d %B %Y")
Out[38]:
DatetimeIndex(['2022-01-13', '2022-01-11', '2022-01-07', '2022-01-06',
               '2022-01-05', '2022-01-04', '2022-01-03', '2021-12-31',
               '2021-12-30', '2021-12-29',
               ...
               '2021-08-09', '2021-08-06', '2021-08-05', '2021-08-04',
               '2021-08-03', '2021-07-27', '2021-07-19', '2021-05-25',
               '2021-05-21', '2021-05-13'],
              dtype='datetime64[ns]', name='Date', length=107, freq=None)
In [39]:
df.index = pd.to_datetime(df.index, format="%A, %d %B %Y")
In [40]:
df.head()
Out[40]:
Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 Equity Group Holdings Ord 0.50 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
Date
2022-01-13 12.90 385.0 320.0 22.20 130.00 54.00 11.80 30.00 59.00 49.55 ... 440.0 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52
2022-01-11 13.80 385.0 320.0 20.55 134.75 44.75 11.90 30.75 59.50 52.00 ... 445.0 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
2022-01-07 13.80 420.0 320.0 21.25 132.00 37.05 11.80 29.05 60.00 53.00 ... 442.0 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
2022-01-06 13.80 420.0 320.0 20.25 130.75 33.70 11.80 29.30 60.00 53.00 ... 442.0 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
2022-01-05 12.85 420.0 320.0 19.95 130.75 30.60 11.75 29.50 59.75 53.00 ... 442.0 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02

5 rows × 60 columns

In [41]:
df['Safaricom PLC Ord 0.05']
Out[41]:
Date
2022-01-13    37.95
2022-01-11    38.45
2022-01-07    39.90
2022-01-06    40.00
2022-01-05    39.55
              ...  
2021-07-27    41.80
2021-07-19    42.60
2021-05-25    42.75
2021-05-21    39.50
2021-05-13    39.40
Name: Safaricom PLC Ord 0.05, Length: 107, dtype: float64
In [42]:
df['Safaricom PLC Ord 0.05'].plot()
Out[42]:
<AxesSubplot:xlabel='Date'>
In [45]:
df['Safaricom PLC Ord 0.05'].plot()
Out[45]:
<module 'matplotlib.pyplot' from 'c:\\users\\just nick\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\matplotlib\\pyplot.py'>
In [46]:
df.index
Out[46]:
DatetimeIndex(['2022-01-13', '2022-01-11', '2022-01-07', '2022-01-06',
               '2022-01-05', '2022-01-04', '2022-01-03', '2021-12-31',
               '2021-12-30', '2021-12-29',
               ...
               '2021-08-09', '2021-08-06', '2021-08-05', '2021-08-04',
               '2021-08-03', '2021-07-27', '2021-07-19', '2021-05-25',
               '2021-05-21', '2021-05-13'],
              dtype='datetime64[ns]', name='Date', length=107, freq=None)
In [52]:
df2 = df.loc['2021-08-03':]
df2
Out[52]:
Eaagads Ltd Ord 1.25 AIMS Kakuzi Ord.5.00 Limuru Tea Co. Ltd Ord 20.00 Sasini Ltd Ord 1.00 Williamson Tea Kenya Ltd Ord 5.00 Car and General (K) Ltd Ord 5.00 Absa Bank Kenya PLC BK Group PLC Diamond Trust Bank Kenya Ltd Ord 4.00 Equity Group Holdings Ord 0.50 ... British American Tobacco Kenya Ltd Ord 10.00 Carbacid Investments Ltd Ord 5.00 East African Breweries Ltd Ord 2.00 Eveready East Africa Ltd Ord.1.00 Flame Tree Group Holdings Ltd Ord 0.825 Kenya Orchards Ltd Ord 5.00 Mumias Sugar Co. Ltd Ord 2.00 Unga Group Ltd Ord 5.00 Safaricom PLC Ord 0.05 Stanlib Fahari I-REIT
Date
2022-01-13 12.90 385.0 320.00 22.20 130.00 54.00 11.80 30.00 59.00 49.55 ... 440.0 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52
2022-01-11 13.80 385.0 320.00 20.55 134.75 44.75 11.90 30.75 59.50 52.00 ... 445.0 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
2022-01-07 13.80 420.0 320.00 21.25 132.00 37.05 11.80 29.05 60.00 53.00 ... 442.0 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
2022-01-06 13.80 420.0 320.00 20.25 130.75 33.70 11.80 29.30 60.00 53.00 ... 442.0 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
2022-01-05 12.85 420.0 320.00 19.95 130.75 30.60 11.75 29.50 59.75 53.00 ... 442.0 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2021-08-09 12.15 415.0 300.00 19.50 134.50 35.00 9.80 32.40 65.75 50.25 ... 445.5 12.25 179.25 0.96 1.32 10.4 0.27 31.00 42.95 6.78
2021-08-06 12.15 415.0 300.00 20.00 134.50 35.00 9.80 32.40 65.75 50.00 ... 454.0 12.25 179.00 0.98 1.32 10.4 0.27 31.00 42.10 6.98
2021-08-05 12.30 415.0 320.00 20.00 134.50 35.00 9.82 31.85 65.00 49.40 ... 450.0 12.20 178.50 0.98 1.31 10.4 0.27 31.00 41.25 6.80
2021-08-04 12.00 415.0 320.00 19.95 135.00 35.00 9.76 29.75 64.00 49.10 ... 455.0 12.00 179.75 0.98 1.30 10.4 0.27 31.00 41.10 6.92
2021-08-03 11.80 415.0 304.75 19.95 134.50 35.00 9.82 29.50 65.00 49.00 ... 450.0 12.00 180.00 0.98 1.31 10.4 0.27 28.30 41.05 6.94

102 rows × 60 columns

In [53]:
plt.plot('Safaricom PLC Ord 0.05',data=df2)
Out[53]:
[<matplotlib.lines.Line2D at 0x1cc49d455b0>]
In [55]:
# let's give our plot a title

plt.plot('Safaricom PLC Ord 0.05',data=df2)
plt.title("Safaricom Daily Stock Prices For Months August 2021 - December 2021")
Out[55]:
Text(0.5, 1.0, 'Safaricom Daily Stock Prices For Months August 2021 - December 2021')
In [56]:
# to set the labels for the x and y axis use the x_label and y_label

plt.plot('Safaricom PLC Ord 0.05',data=df2)

# let's give our plot a title

plt.title("Safaricom Daily Stock Prices For Months August 2021 - December 2021")

# set the xlabel

plt.xlabel("Date")
plt.ylabel("Stock Price")
Out[56]:
Text(0, 0.5, 'Stock Price')
In [61]:
# to set the labels for the x and y axis use the x_label and y_label


# adjust the size of the chart

plt.figure(figsize=(12,8))

# let's give our plot a title

plt.title("Safaricom Daily Stock Prices For Months August 2021 - December 2021")

# set the xlabel

plt.xlabel("Date")

# set the ylabel

plt.ylabel("Stock Price")

plt.plot('Safaricom PLC Ord 0.05',data=df2)

plt.show()
In [62]:
# method 2 set_size_inches()


# let's give our plot a title

plt.title("Safaricom Daily Stock Prices For Months August 2021 - December 2021")

# set the xlabel

plt.xlabel("Date")

# set the ylabel

plt.ylabel("Stock Price")

plt.plot('Safaricom PLC Ord 0.05',data=df2)

fig = plt.gcf()

fig.set_size_inches(10,8)

plt.show()
In [63]:
# third method rcParams

# this will change for all the following charts

plt.rcParams['figure.figsize'] = (3, 2)

# let's give our plot a title

plt.title("Safaricom Daily Stock Prices For Months August 2021 - December 2021")

# set the xlabel

plt.xlabel("Date")

# set the ylabel

plt.ylabel("Stock Price")

plt.plot('Safaricom PLC Ord 0.05',data=df2)

fig = plt.gcf()

plt.show()
In [64]:
plt.rcParams['figure.figsize'] = (12, 8)
In [73]:
# Saving a Plot

# let's give our plot a title

plt.title("Safaricom Daily Stock Prices For Months August 2021 - December 2021")

# set the xlabel

plt.xlabel("Date")

# set the ylabel

plt.ylabel("Stock Price")

plt.plot('Safaricom PLC Ord 0.05',data=df2,color="green")

fig = plt.gcf()

fig.set_size_inches(12,8)

plt.savefig('safaricom_stocks.png')

plt.show()
In [77]:
plt.title('Equity Daily Stock Prices For Months August 2021 - December 2021')

# set the xlabel

plt.xlabel("Date")

# set the ylabel

plt.ylabel("Stock Price")

plt.plot('Equity Group Holdings Ord 0.50',data=df2, color="brown")
plt.grid()

Plot Multiple Line Plots in Matplotlib

In [79]:
fig, ax = plt.subplots(figsize=(10,8))

ax.plot('Safaricom PLC Ord 0.05',data=df2,color="green",label="SCOM")
ax.plot('Equity Group Holdings Ord 0.50',data=df2,color="brown",label="EQTY")
ax.legend(loc = 'upper left')
plt.show()
In [80]:
df.columns
Out[80]:
Index(['Eaagads Ltd Ord 1.25 AIMS', 'Kakuzi Ord.5.00',
       'Limuru Tea Co. Ltd Ord 20.00', 'Sasini Ltd Ord 1.00',
       'Williamson Tea Kenya Ltd Ord 5.00', 'Car and General (K) Ltd Ord 5.00',
       'Absa Bank Kenya PLC', 'BK Group PLC',
       'Diamond Trust Bank Kenya Ltd Ord 4.00',
       'Equity Group Holdings Ord 0.50', 'HF Group Ltd Ord 5.00',
       'I&M Holdings Ltd Ord 1.00', 'KCB Group Ltd Ord 1.00',
       'National Bank of Kenya Ltd Ord 5.00', 'NCBA Group PLC',
       ' Stanbic Holdings Plc. ord.5.00',
       'Standard Chartered Bank Ltd Ord 5.00',
       'The Co-operative Bank of Kenya Ltd Ord 1.00', 'Express Ltd Ord 5.00',
       'Kenya Airways Ltd Ord 5.00', 'Longhorn Publishers Ltd',
       ' Nairobi Business Ventures Ltd', 'Nation Media Group Ord. 2.50',
       'Sameer Africa PLC Ord 5.00', 'Scangroup  Ltd Ord 1.00',
       'Standard Group  Ltd Ord 5.00',
       'TPS Eastern Africa (Serena) Ltd Ord 1.00 ',
       'Uchumi Supermarket Ltd Ord 5.00', 'Athi River Mining Ord 5.00',
       'Bamburi Cement Ltd Ord 5.00', 'Crown Paints Kenya PLC. 0rd 5.00',
       'E.A.Cables Ltd Ord 0.50', 'E.A.Portland Cement Ltd Ord 5.00',
       'KenGen Ltd  Ord. 2.50', ' Kenya Power & Lighting  Co Ltd',
       'Total Kenya Ltd Ord 5.00', 'Umeme Ltd Ord 0.50',
       'Britam Holdings Ltd Ord 0.10', 'CIC Insurance Group Ltd Ord 1.00',
       'Jubilee Holdings Ltd Ord 5.00',
       'Kenya Re-Insurance Corporation Ltd Ord 2.50',
       'Liberty Kenya Holdings Ltd', 'Sanlam Kenya PLC 0rd 5.00',
       'Centum Investment Co Ltd Ord 0.50', ' Home Afrika Ltd Ord 1.00',
       'Kurwitu Ventures', 'Olympia Capital Holdings ltd Ord 5.00',
       'Trans-Century Ltd', 'Nairobi Securities Exchange Ltd Ord 4.00',
       'B.O.C Kenya Ltd Ord 5.00',
       'British American Tobacco Kenya Ltd Ord 10.00',
       'Carbacid Investments Ltd Ord 5.00',
       'East African Breweries Ltd Ord 2.00',
       'Eveready East Africa Ltd Ord.1.00',
       'Flame Tree Group Holdings Ltd Ord 0.825',
       'Kenya Orchards Ltd Ord 5.00', 'Mumias Sugar Co. Ltd Ord 2.00',
       'Unga Group Ltd Ord 5.00', 'Safaricom PLC Ord 0.05',
       'Stanlib Fahari I-REIT'],
      dtype='object')
In [84]:
tickers = ['EGAD','KUKZ','LIMT',
           'SASN','WTK', 'CGEN',
           'ABSA','BKG','DTK',
           'EQTY','HFCK','IMH', 
           'KCB','NBK', 'NCBA',
           'SBIC','SCBK','COOP',
           'XPRS','KQ','LKL',
           'NBV', 'NMG','SMER',
           'SCAN','SGL','TPSE',
           'UCHM','ARM','BAMB',
           'CRWN','CABL','PORT',
           'KEGN','KPLC','TOTL',
           'UMME','BRIT','CIC',
           'JUB','KNRE','LBTY', 
           'SLAM','CTUM','HAFR',
           'KURV','OCH','TCL',
           'NSE','BOC','BAT',
           'CARB','EABL','EVRD',
           'FTGH','ORCH', 'MSC',
           'UNGA','SCOM','FAHR']

tickers
Out[84]:
['EGAD',
 'KUKZ',
 'LIMT',
 'SASN',
 'WTK',
 'CGEN',
 'ABSA',
 'BKG',
 'DTK',
 'EQTY',
 'HFCK',
 'IMH',
 'KCB',
 'NBK',
 'NCBA',
 'SBIC',
 'SCBK',
 'COOP',
 'XPRS',
 'KQ',
 'LKL',
 'NBV',
 'NMG',
 'SMER',
 'SCAN',
 'SGL',
 'TPSE',
 'UCHM',
 'ARM',
 'BAMB',
 'CRWN',
 'CABL',
 'PORT',
 'KEGN',
 'KPLC',
 'TOTL',
 'UMME',
 'BRIT',
 'CIC',
 'JUB',
 'KNRE',
 'LBTY',
 'SLAM',
 'CTUM',
 'HAFR',
 'KURV',
 'OCH',
 'TCL',
 'NSE',
 'BOC',
 'BAT',
 'CARB',
 'EABL',
 'EVRD',
 'FTGH',
 'ORCH',
 'MSC',
 'UNGA',
 'SCOM',
 'FAHR']
In [86]:
df.columns = tickers
In [87]:
df.head()
Out[87]:
EGAD KUKZ LIMT SASN WTK CGEN ABSA BKG DTK EQTY ... BAT CARB EABL EVRD FTGH ORCH MSC UNGA SCOM FAHR
Date
2022-01-13 12.90 385.0 320.0 22.20 130.00 54.00 11.80 30.00 59.00 49.55 ... 440.0 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52
2022-01-11 13.80 385.0 320.0 20.55 134.75 44.75 11.90 30.75 59.50 52.00 ... 445.0 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
2022-01-07 13.80 420.0 320.0 21.25 132.00 37.05 11.80 29.05 60.00 53.00 ... 442.0 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
2022-01-06 13.80 420.0 320.0 20.25 130.75 33.70 11.80 29.30 60.00 53.00 ... 442.0 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
2022-01-05 12.85 420.0 320.0 19.95 130.75 30.60 11.75 29.50 59.75 53.00 ... 442.0 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02

5 rows × 60 columns

In [92]:
df2.columns = tickers

Matplotlib Subplot¶

With the subplot() function you can draw multiple plots in one figure

In [98]:
plt.subplot(1,2,1)

plt.plot('SCOM',data=df2,color="green")
plt.title('SCOM')


plt.subplot(1,2,2)
plt.plot('EQTY',data=df2,color="brown")
plt.title("EQTY")
plt.show()

Let's create plots of the banking sector

  • KCB
  • NCBA
  • NBK
  • EQTY
  • COOP
  • SBIC
  • SCBK
  • ABSA
  • HFCK
  • DTK
  • IMH
  • BKG
In [99]:
df2.columns
Out[99]:
Index(['EGAD', 'KUKZ', 'LIMT', 'SASN', 'WTK', 'CGEN', 'ABSA', 'BKG', 'DTK',
       'EQTY', 'HFCK', 'IMH', 'KCB', 'NBK', 'NCBA', 'SBIC', 'SCBK', 'COOP',
       'XPRS', 'KQ', 'LKL', 'NBV', 'NMG', 'SMER', 'SCAN', 'SGL', 'TPSE',
       'UCHM', 'ARM', 'BAMB', 'CRWN', 'CABL', 'PORT', 'KEGN', 'KPLC', 'TOTL',
       'UMME', 'BRIT', 'CIC', 'JUB', 'KNRE', 'LBTY', 'SLAM', 'CTUM', 'HAFR',
       'KURV', 'OCH', 'TCL', 'NSE', 'BOC', 'BAT', 'CARB', 'EABL', 'EVRD',
       'FTGH', 'ORCH', 'MSC', 'UNGA', 'SCOM', 'FAHR'],
      dtype='object')
In [101]:
df2.loc[:,'ABSA':'COOP']
Out[101]:
ABSA BKG DTK EQTY HFCK IMH KCB NBK NCBA SBIC SCBK COOP
Date
2022-01-13 11.80 30.00 59.00 49.55 3.64 21.00 45.25 4.12 25.70 88.5 129.50 12.55
2022-01-11 11.90 30.75 59.50 52.00 3.81 21.50 45.85 4.12 25.95 87.5 130.00 12.80
2022-01-07 11.80 29.05 60.00 53.00 3.81 21.40 46.00 4.12 25.95 87.0 130.50 12.95
2022-01-06 11.80 29.30 60.00 53.00 3.89 21.45 45.90 4.12 25.90 87.0 130.75 13.00
2022-01-05 11.75 29.50 59.75 53.00 3.81 21.45 45.50 4.12 25.55 87.0 130.00 13.00
... ... ... ... ... ... ... ... ... ... ... ... ...
2021-08-09 9.80 32.40 65.75 50.25 3.70 22.50 46.60 4.12 25.85 86.5 131.50 13.65
2021-08-06 9.80 32.40 65.75 50.00 3.75 22.55 46.75 4.12 26.20 92.5 131.00 13.65
2021-08-05 9.82 31.85 65.00 49.40 3.71 22.30 46.45 4.12 26.20 90.0 130.00 13.55
2021-08-04 9.76 29.75 64.00 49.10 3.66 22.70 46.20 4.12 26.25 90.0 132.00 13.45
2021-08-03 9.82 29.50 65.00 49.00 3.74 22.75 45.95 4.12 26.65 88.5 130.00 13.55

102 rows × 12 columns

We need 12 plots.

let's do 6 rows 2 columns

In [103]:
plt.subplot(6,2,1)
plt.plot('ABSA',data=df2)

plt.subplot(6,2,2)
plt.plot('BKG',data=df2)

plt.subplot(6,2,3)
plt.plot('DTK',data=df2)
Out[103]:
[<matplotlib.lines.Line2D at 0x1cc4b56f160>]
In [105]:
banking_df = df2.loc[:,'ABSA':'COOP']
In [106]:
banking_df.columns
Out[106]:
Index(['ABSA', 'BKG', 'DTK', 'EQTY', 'HFCK', 'IMH', 'KCB', 'NBK', 'NCBA',
       'SBIC', 'SCBK', 'COOP'],
      dtype='object')
In [107]:
bank_cols = banking_df.columns
for bank in bank_cols:
    print(bank)
ABSA
BKG
DTK
EQTY
HFCK
IMH
KCB
NBK
NCBA
SBIC
SCBK
COOP
In [129]:
bank_cols = banking_df.columns

font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'size': 16,
        }

for idx,bank in enumerate(bank_cols,start=1):
    plt.subplot(6,2,idx)
    plt.title(bank,fontdict=font)
    plt.grid()
    plt.plot(bank,data=df2)
    
fig = plt.gcf()
fig.set_size_inches(16,30)
plt.show()
In [131]:
banking_df.corr(method='pearson')
Out[131]:
ABSA BKG DTK EQTY HFCK IMH KCB NBK NCBA SBIC SCBK COOP
ABSA 1.000000 -0.247357 -0.367356 0.051548 0.089564 -0.497121 -0.242094 NaN -0.071353 -0.079066 -0.211586 -0.367982
BKG -0.247357 1.000000 0.733606 0.431693 -0.363877 0.685030 0.722452 NaN 0.546149 0.160496 0.769506 0.777537
DTK -0.367356 0.733606 1.000000 0.377873 -0.472187 0.907300 0.865433 NaN 0.826353 0.079410 0.751985 0.946788
EQTY 0.051548 0.431693 0.377873 1.000000 0.177967 0.468084 0.661149 NaN 0.333037 0.173888 0.495452 0.484453
HFCK 0.089564 -0.363877 -0.472187 0.177967 1.000000 -0.312478 -0.263884 NaN -0.522405 0.199610 -0.288670 -0.469807
IMH -0.497121 0.685030 0.907300 0.468084 -0.312478 1.000000 0.850515 NaN 0.748388 0.159981 0.746789 0.872273
KCB -0.242094 0.722452 0.865433 0.661149 -0.263884 0.850515 1.000000 NaN 0.761396 0.163069 0.679501 0.902445
NBK NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
NCBA -0.071353 0.546149 0.826353 0.333037 -0.522405 0.748388 0.761396 NaN 1.000000 0.134429 0.719180 0.771551
SBIC -0.079066 0.160496 0.079410 0.173888 0.199610 0.159981 0.163069 NaN 0.134429 1.000000 0.313971 0.040135
SCBK -0.211586 0.769506 0.751985 0.495452 -0.288670 0.746789 0.679501 NaN 0.719180 0.313971 1.000000 0.727922
COOP -0.367982 0.777537 0.946788 0.484453 -0.469807 0.872273 0.902445 NaN 0.771551 0.040135 0.727922 1.000000
In [133]:
corr_df = banking_df.corr(method="pearson")
In [18]:
import seaborn as sns
In [135]:
plt.figure(figsize=(13, 8))
sns.heatmap(corr_df, annot=True, cmap='RdYlGn')
plt.figure()
Out[135]:
<Figure size 864x576 with 0 Axes>
<Figure size 864x576 with 0 Axes>
In [137]:
banking_df.head()
Out[137]:
ABSA BKG DTK EQTY HFCK IMH KCB NBK NCBA SBIC SCBK COOP
Date
2022-01-13 11.80 30.00 59.00 49.55 3.64 21.00 45.25 4.12 25.70 88.5 129.50 12.55
2022-01-11 11.90 30.75 59.50 52.00 3.81 21.50 45.85 4.12 25.95 87.5 130.00 12.80
2022-01-07 11.80 29.05 60.00 53.00 3.81 21.40 46.00 4.12 25.95 87.0 130.50 12.95
2022-01-06 11.80 29.30 60.00 53.00 3.89 21.45 45.90 4.12 25.90 87.0 130.75 13.00
2022-01-05 11.75 29.50 59.75 53.00 3.81 21.45 45.50 4.12 25.55 87.0 130.00 13.00
In [138]:
# drop NBK
banking_df.drop('NBK',axis="columns")
Out[138]:
ABSA BKG DTK EQTY HFCK IMH KCB NCBA SBIC SCBK COOP
Date
2022-01-13 11.80 30.00 59.00 49.55 3.64 21.00 45.25 25.70 88.5 129.50 12.55
2022-01-11 11.90 30.75 59.50 52.00 3.81 21.50 45.85 25.95 87.5 130.00 12.80
2022-01-07 11.80 29.05 60.00 53.00 3.81 21.40 46.00 25.95 87.0 130.50 12.95
2022-01-06 11.80 29.30 60.00 53.00 3.89 21.45 45.90 25.90 87.0 130.75 13.00
2022-01-05 11.75 29.50 59.75 53.00 3.81 21.45 45.50 25.55 87.0 130.00 13.00
... ... ... ... ... ... ... ... ... ... ... ...
2021-08-09 9.80 32.40 65.75 50.25 3.70 22.50 46.60 25.85 86.5 131.50 13.65
2021-08-06 9.80 32.40 65.75 50.00 3.75 22.55 46.75 26.20 92.5 131.00 13.65
2021-08-05 9.82 31.85 65.00 49.40 3.71 22.30 46.45 26.20 90.0 130.00 13.55
2021-08-04 9.76 29.75 64.00 49.10 3.66 22.70 46.20 26.25 90.0 132.00 13.45
2021-08-03 9.82 29.50 65.00 49.00 3.74 22.75 45.95 26.65 88.5 130.00 13.55

102 rows × 11 columns

In [139]:
# update the original df
banking_df.drop('NBK',axis="columns", inplace=True)
In [140]:
banking_df.head()
Out[140]:
ABSA BKG DTK EQTY HFCK IMH KCB NCBA SBIC SCBK COOP
Date
2022-01-13 11.80 30.00 59.00 49.55 3.64 21.00 45.25 25.70 88.5 129.50 12.55
2022-01-11 11.90 30.75 59.50 52.00 3.81 21.50 45.85 25.95 87.5 130.00 12.80
2022-01-07 11.80 29.05 60.00 53.00 3.81 21.40 46.00 25.95 87.0 130.50 12.95
2022-01-06 11.80 29.30 60.00 53.00 3.89 21.45 45.90 25.90 87.0 130.75 13.00
2022-01-05 11.75 29.50 59.75 53.00 3.81 21.45 45.50 25.55 87.0 130.00 13.00
In [141]:
corr_df = banking_df.corr(method="pearson")
In [142]:
corr_df.head()
Out[142]:
ABSA BKG DTK EQTY HFCK IMH KCB NCBA SBIC SCBK COOP
ABSA 1.000000 -0.247357 -0.367356 0.051548 0.089564 -0.497121 -0.242094 -0.071353 -0.079066 -0.211586 -0.367982
BKG -0.247357 1.000000 0.733606 0.431693 -0.363877 0.685030 0.722452 0.546149 0.160496 0.769506 0.777537
DTK -0.367356 0.733606 1.000000 0.377873 -0.472187 0.907300 0.865433 0.826353 0.079410 0.751985 0.946788
EQTY 0.051548 0.431693 0.377873 1.000000 0.177967 0.468084 0.661149 0.333037 0.173888 0.495452 0.484453
HFCK 0.089564 -0.363877 -0.472187 0.177967 1.000000 -0.312478 -0.263884 -0.522405 0.199610 -0.288670 -0.469807
In [158]:
# customize text
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'size': 26,
        }

plt.figure(figsize=(16,8))
plt.title("Banking Sector Stock Price Correlation Plot", fontdict=font)
cmap = ["mako","PiYG","YlGnBu","Blues"]
sns.heatmap(corr_df, annot=True, cmap=cmap[np.random.randint(len(cmap))])
plt.figure()
plt.show()
<Figure size 864x576 with 0 Axes>
In [159]:
 
    
# customize text
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'size': 26,
        }

plt.figure(figsize=(16,8))
plt.title("Banking Sector Stock Price Correlation Plot", fontdict=font)
cmap = ["mako","PiYG","YlGnBu","Blues"]
sns.heatmap(corr_df, annot=True, cmap=cmap[np.random.randint(len(cmap))],linewidth=1, linecolor='w', square=True)
plt.figure()
plt.show()
<Figure size 864x576 with 0 Axes>
In [20]:
os.listdir('..')
Out[20]:
['.ipynb_checkpoints',
 'bmi-solution.ipynb',
 'cleaned_stock.csv',
 'comprehensive-guide-to-pandas.ipynb',
 'conditional expressions in python.ipynb',
 'data-analysis-with-python-pandas.ipynb',
 'data-dealing',
 'data-dealing.py',
 'denaco-manu-python-curriculum - Sheet1.pdf',
 'df-from-dict.py',
 'fizz_buzz.py',
 'functions.ipynb',
 'Intro to python',
 'just-enough-numpy.ipynb',
 'LCM.ipynb',
 'loops and iterations in python.ipynb',
 'mastery_of_pandas.ipynb',
 'modules and standard library.ipynb',
 'our_new_dir',
 'pandas tuts',
 'Pandas-Demo',
 'pandas-student-workbook.ipynb',
 'python-resources.txt',
 'python-string-formating.py',
 'Python.docx',
 'student_copy_pandas_workbook.ipynb',
 'student_workbook_stocks.ipynb',
 'train.csv',
 'Untitled.ipynb',
 'users.csv',
 'vehicle_data.csv',
 'vehicle_data_big.csv',
 'Visualization with Matplotlib']
In [21]:
clean_df = pd.read_csv('../cleaned_stock.csv',index_col=0)
clean_df.head()
Out[21]:
EGAD KUKZ LIMT SASN WTK CGEN ABSA BKG DTK EQTY ... BAT CARB EABL EVRD FTGH ORCH MSC UNGA SCOM FAHR
Date
2022-01-13 12.90 385.0 320.0 22.20 130.00 54.00 11.80 30.00 59.00 49.55 ... 440.0 10.80 151.50 0.96 1.34 10.4 0.27 27.10 37.95 6.52
2022-01-11 13.80 385.0 320.0 20.55 134.75 44.75 11.90 30.75 59.50 52.00 ... 445.0 10.85 161.00 0.88 1.31 10.4 0.27 27.65 38.45 6.56
2022-01-07 13.80 420.0 320.0 21.25 132.00 37.05 11.80 29.05 60.00 53.00 ... 442.0 10.90 164.75 0.94 1.30 10.4 0.27 27.65 39.90 6.38
2022-01-06 13.80 420.0 320.0 20.25 130.75 33.70 11.80 29.30 60.00 53.00 ... 442.0 10.90 160.75 0.99 1.29 10.4 0.27 27.65 40.00 6.40
2022-01-05 12.85 420.0 320.0 19.95 130.75 30.60 11.75 29.50 59.75 53.00 ... 442.0 10.90 163.75 0.99 1.26 10.4 0.27 27.65 39.55 6.02

5 rows × 60 columns

In [30]:
clean_df.tail()
Out[30]:
EGAD KUKZ LIMT SASN WTK CGEN ABSA BKG DTK EQTY ... BAT CARB EABL EVRD FTGH ORCH MSC UNGA SCOM FAHR
Date
2021-07-27 11.5 410.00 280.0 19.95 134.00 35.2 9.74 29.0 66.25 48.50 ... 469.50 12.05 184.00 1.01 1.33 10.4 0.27 29.0 41.80 6.94
2021-07-19 11.0 372.75 290.0 19.20 152.25 31.0 10.00 28.0 69.00 48.55 ... 445.00 12.00 187.75 1.04 1.32 10.4 0.27 27.1 42.60 6.66
2021-05-25 15.0 360.00 340.0 18.40 145.00 24.2 9.22 26.0 63.25 42.05 ... 464.75 10.15 181.00 0.99 1.40 10.4 0.27 28.0 42.75 5.96
2021-05-21 15.0 360.00 340.0 18.55 145.00 22.0 9.10 26.0 63.75 41.50 ... 470.00 10.40 174.00 1.04 1.35 10.4 0.27 28.0 39.50 5.98
2021-05-13 15.0 360.00 340.0 18.80 146.75 20.5 8.80 26.0 65.00 40.95 ... 471.00 10.90 172.25 1.09 1.41 10.4 0.27 31.8 39.40 6.10

5 rows × 60 columns

In [38]:
banking_df = clean_df.loc[:'2021-08-01','ABSA':'COOP'].copy()
banking_df.head()
Out[38]:
ABSA BKG DTK EQTY HFCK IMH KCB NBK NCBA SBIC SCBK COOP
Date
2022-01-13 11.80 30.00 59.00 49.55 3.64 21.00 45.25 4.12 25.70 88.5 129.50 12.55
2022-01-11 11.90 30.75 59.50 52.00 3.81 21.50 45.85 4.12 25.95 87.5 130.00 12.80
2022-01-07 11.80 29.05 60.00 53.00 3.81 21.40 46.00 4.12 25.95 87.0 130.50 12.95
2022-01-06 11.80 29.30 60.00 53.00 3.89 21.45 45.90 4.12 25.90 87.0 130.75 13.00
2022-01-05 11.75 29.50 59.75 53.00 3.81 21.45 45.50 4.12 25.55 87.0 130.00 13.00
In [39]:
banking_df.drop('NBK',axis='columns',inplace=True)

Returns¶

In [40]:
returns = (np.log(banking_df).diff()).dropna()
returns.head()
Out[40]:
ABSA BKG DTK EQTY HFCK IMH KCB NCBA SBIC SCBK COOP
Date
2022-01-11 0.008439 0.024693 0.008439 0.048261 0.045646 0.023530 0.013173 0.009681 -0.011364 0.003854 0.019725
2022-01-07 -0.008439 -0.056872 0.008368 0.019048 0.000000 -0.004662 0.003266 0.000000 -0.005731 0.003839 0.011651
2022-01-06 0.000000 0.008569 0.000000 0.000000 0.020780 0.002334 -0.002176 -0.001929 0.000000 0.001914 0.003854
2022-01-05 -0.004246 0.006803 -0.004175 0.000000 -0.020780 0.000000 -0.008753 -0.013606 0.000000 -0.005753 0.000000
2022-01-04 0.004246 -0.088553 0.000000 0.004706 0.023347 0.000000 -0.009939 -0.003922 0.000000 0.001921 -0.003854
In [41]:
returns_corr = returns.corr()
returns_corr
Out[41]:
ABSA BKG DTK EQTY HFCK IMH KCB NCBA SBIC SCBK COOP
ABSA 1.000000 0.022762 0.136674 0.146987 0.024985 0.076334 0.187388 0.148608 0.020974 0.146331 0.052433
BKG 0.022762 1.000000 -0.131985 0.240038 -0.164986 -0.011852 0.209560 0.178862 -0.088376 -0.042003 0.211816
DTK 0.136674 -0.131985 1.000000 -0.009057 0.065736 -0.047307 0.050994 0.084801 -0.070782 -0.101213 0.051639
EQTY 0.146987 0.240038 -0.009057 1.000000 0.081408 0.023325 0.554541 0.121779 0.069262 0.000553 0.347760
HFCK 0.024985 -0.164986 0.065736 0.081408 1.000000 0.098241 0.139988 0.028760 0.070278 -0.027010 0.011309
IMH 0.076334 -0.011852 -0.047307 0.023325 0.098241 1.000000 0.035567 0.041591 -0.062302 0.115253 -0.011832
KCB 0.187388 0.209560 0.050994 0.554541 0.139988 0.035567 1.000000 0.069175 0.102898 -0.052557 0.225739
NCBA 0.148608 0.178862 0.084801 0.121779 0.028760 0.041591 0.069175 1.000000 0.069276 0.141610 0.116900
SBIC 0.020974 -0.088376 -0.070782 0.069262 0.070278 -0.062302 0.102898 0.069276 1.000000 0.203313 -0.074111
SCBK 0.146331 -0.042003 -0.101213 0.000553 -0.027010 0.115253 -0.052557 0.141610 0.203313 1.000000 0.117065
COOP 0.052433 0.211816 0.051639 0.347760 0.011309 -0.011832 0.225739 0.116900 -0.074111 0.117065 1.000000
In [42]:
# customize text
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'size': 26,
        }

plt.figure(figsize=(16,8))
plt.title("Banking Sector Stock Returns Correlation Plot", fontdict=font)
cmap = ["mako","PiYG","YlGnBu","Blues"]
sns.heatmap(returns_corr, annot=True, cmap=cmap[np.random.randint(len(cmap))],linewidth=1, linecolor='w', square=True)
plt.figure()
plt.show()
<Figure size 432x288 with 0 Axes>
In [48]:
plt.style.use('seaborn')

plt.scatter(banking_df['EQTY'],banking_df['KCB'])
plt.show()
In [49]:
reg = np.polyfit(banking_df['EQTY'], banking_df['KCB'], deg = 1)
reg
Out[49]:
array([ 0.67419028, 11.45556244])
In [51]:
trend = np.polyval(reg, banking_df['EQTY'])
plt.scatter(banking_df['EQTY'], banking_df['KCB'])
plt.xlabel("EQTY")
plt.ylabel("KCB")
plt.plot(banking_df["EQTY"], trend, 'r');
In [ ]:
 
In [ ]:
 

References¶

  1. https://stackabuse.com/matplotlib-plot-multiple-line-plots-same-and-different-scales/

  2. https://www.w3schools.com/python/matplotlib_subplot.asp