#5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices (Python Financial Analysis)

wsh
Python Financial Analysis
6 min readAug 9, 2021

#5.1 COVID-19 Impacts by Sector (Compare weighted average prices)

Python Financial Analysis | Home

Introduction

Over the last 4 stories, we’ve seen how to display stock prices and their weighted averages by making graphs in Python. Now that we can handle price data, we can make more practical financial analysis.

Today’s top is about analyzing the impact of the COVID-19 pandemic on the stock market by sectors. If you’ve been reading newspapers, you should know what sector survived and what sector did not. But displaying them with numerical values leads you to more advanced investment strategies.

I guess not all the readers of this story are not interested in Python. So, unlike the previous stories, I’ll make it enjoyable for any kind of people!

You can download the data set from this link
https://drive.google.com/drive/folders/1Ux2u1s5mctYiywS08sv7_3_PbnWd8v0G?usp=sharing

PDF document of this story is also available
https://drive.google.com/drive/folders/15q64knsDYErXiPyy-jK9ViSVN4ysDaoR?usp=sharing

List of articles

1. Python Financial Analysis

1 Read fundamental data from a CSV in Python
2 Handling table like data in Python with DataFrame
3 Make graphs of stock price in Python
4.1 Make custom market index — prerequisites
4.2 Make custom market index — make your own index
4.3 Make custom market index — market cap based index
5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices
5.2 Analyze COVID-19 Impacts by Market Caps in Python — compare weighted average prices
5.3 Find companies that lost or gained from the COVID19 pandemic

2. Python Data Analysis Basics (easiest ways)

Python “datetime” in the easiest way (how to handle dates in data science with Python)
Python DataFrame slicing in the easiest way (How to find a company from 5000 companies)

Sector performances

1. Earlier stage of pandemic (Jan — May)

The image below shows the performance of the most 7 common sectors after the COVID-19 pandemic. This is an index of each sector which is calculated as weighted average of prices according to the market caps. That means, larger companies have more influence on the index than smaller companies. In addition to that, to see the indices in a relative form, we adjust all the indices to 1.0 on Jan 1, 2020.

You can see that “Oils Energy” got the most severe impact from the pandemic. Although other sectors keep their value at the beginning of the pandemic (about until Feb 20), Oils Energy started to drop at the earlier stage. People didn’t think the COVID-19 would become a pandemic at this phase, but investors were already worried about the future.

The biggest reason why Oils Energy got the largest impact is because airplanes could not fly between countries.

Another thing you notice is, “Computer and Technology” was still gaining at the earlier stage. This could be because this sector is always growing over this decade, or may be because investors expected demands for this sector would grow due to the pandemic.

2. Performance after the impact (Jan — Next May)

The next is the overall performance by sectors. You may notice that the “Auto Tires Trucks” has a huge gain after the pandemic, although people lost their jobs and could not buy new cars. The gain mainly comes from the leap of Tesla (TSLA), whose stock price got more than five times larger. Tesla started to increase its price before the emerge of the COVID-19.

Computer and Technology has also big gain during the pandemic. This is because people now work at home and use PCs and gaming consoles more than before. Especially, demands for chips was huge.

Oils Energy and Aerospace are still struggling to get out the slumps. We can’t expect growth of the two sectors unless the pandemic comes to an end and people start to travel overseas.

Python script

1. prerequisites

If can’t understand what’s the code is doing, I recommend you to read the previous stories from the “Custom Market Index”. The code is almost the same.

As usual, we first import the packages, and read the CSV files “pricedata_reshaped.csv” and “meta.csv”, which can be downloaded from the link on the introduction.

2. Make the x-axis

The next is converting string in the column “date” into datetime objects such that it can be used as the x-axis of the graph. The results are saved as a new column “datetime”.

3. Handling missing values

Then we fill NaN values with zeros. As I explained in the previous stories, NaN values are missing values. Any mathematical operations with NaN values become NaNs. Thus, we have to replace them with zeros before calculating weighted average of prices.

4. Crop dataset

The next is cropping the DataFrame “price” such that it just includes prices during the COVID-19 pandemic. The condition is done with a datetime object. It can be made with the “datetime.datetime.strptime()” function. We give a specific date in string. The price range here is from “2020–01–01” to “2020–03–20”. You can click the image to scale it up if it’s too small.

5. Select sectors

Then we define the sectors to display on the graph. I selected just 7 sectors for good visibility. But you can also select the whole sectors. This is your choice.

6. Generate indices and graphs

The next is a little bit confusing (please follow the previous stories for more details!). In the next code, we calculate the weighted averages of prices, save their performances, and display the graphs as images. Because this time, unlike the last story, we want to save the values of indices at “2020–03–20” in a form of table, we make a dictionary “gains” at the first line. We can convert it into a CSV file later.

Because we have to calculate the indices (weighted sum of prices) for each sector and display then on a single image, we iterate over the sectors “SECTORS”. Then we crop the DataFrame “meta” such that it include just a specific sector and exclude companies whose market caps are less than $1M. At the next line, we calculate the total market caps of this sector, since the weights of the index is according to the market caps. We have to divide each market cap by the sector total. “TICKERS_SEC” is tickers of companies that belong to this sector. At the next line, we make an empty column in the DataFrame “price” as a save location of the index.

Inside the next “for” loop is the codes that actually calculates the weighted sum of prices. “w” is the weight and “p” is the price. The weight is taken from the market cap of the company, and the price is rescaled by its latest price. Then we append their multiplication to the created new column. After making the index for each sector, we make its graph with the “plot()” function.

Then we save the figure as an image and make a CSV file that contains the performance of each sector. Remember, the performance is saved in the Python dictionary “gains”. Thus, we have to convert it to a “Series” first. This should be the first time you see “Series”, but you can think is as a 1 dimensional version of DataFrame, or a table with just one column. We then save the Series as a CSV file “pandemic_impacts_by_sectors-7sec_untilmay.csv”.

7. Run it!

Open the console and type “python sectorwise_pandemic_impact.py”

Full Python code

You can download the dataset from the link above.

Other Links

Python Financial Analysis | Home
Python Data Analysis | Home

--

--