#First: import the libraries we will need for this project:
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.style as style
# To enable Jupyter to display graphs
%matplotlib inline
#Second: import the csv file into a pandas DataFrame:
exchange_rates = pd.read_csv("euro-daily-hist_1999_2020.csv")
#Third: visualize some rows:
exchange_rates #as alternative we could print rows using df.head() and df.tail()
#Fourth: check some data details (type, null values, ...):
exchange_rates.info()
exchange_rates.rename({"[US dollar ]":"US_dollar", r"Period\Unit:":"Time"}, axis=1, inplace=True)
#In order to avoid `unicode error` in the 2nd column name, we need to use "r" before the string name
#as we have "\U..." it is interpreted as eight-character Unicode escape, if we don't put "r" or use double "\\"
exchange_rates.columns # to visualize the changes
#format of information in this column is: 2021-01-08
exchange_rates["Time"]=pd.to_datetime(exchange_rates["Time"], format="%Y-%m-%d")
exchange_rates["Time"].dtype #checking the changes
exchange_rates.sort_values("Time", ascending=True,inplace=True) # sort values by time
exchange_rates.reset_index(drop=True, inplace=True) # reset the index (and drop the initial index)
exchange_rates # visualize the changes
euro_to_dollar=exchange_rates.copy()[["Time", "US_dollar"]]
euro_to_dollar # checking the new Dataframe
euro_to_dollar.info()
euro_to_dollar["US_dollar"].value_counts()
euro_to_dollar=euro_to_dollar[euro_to_dollar["US_dollar"]!="-"]
euro_to_dollar #checking our new dataset:
euro_to_dollar.loc[:,"US_dollar"]=euro_to_dollar.loc[:,"US_dollar"].astype(float)
euro_to_dollar.info()
plt.plot(euro_to_dollar["Time"],euro_to_dollar["US_dollar"])
plt.show()
euro_to_dollar['rolling_mean'] = euro_to_dollar['US_dollar'].rolling(30).mean()
plt.plot(euro_to_dollar['Time'], euro_to_dollar['rolling_mean'])
plt.show()
style.use("fivethirtyeight")
decrease_period=euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2007) & (euro_to_dollar['Time'].dt.year <= 2019)]
fig, ax = plt.subplots(figsize=(10,3))
ax.plot(decrease_period['Time'], decrease_period['rolling_mean'],
color='#af0b1e',linewidth=2)
ax.axvspan(xmin=13950, xmax=14140, ymin=0.09,color="grey", alpha=0.3)
ax.axvspan(xmin=15950, xmax=16850, ymin=0.09, color='grey',alpha=0.3)
ax.axvspan(xmin=17550, xmax=18250, ymin=0.09, color='grey',alpha=0.3)
plt.show()
financial_crisis=euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2006) & (euro_to_dollar['Time'].dt.year <= 2010)]
#Create the plot area and structure
fig, (ax1, ax2) = plt.subplots(figsize=(8,3), nrows=2, ncols=1)
axes=[ax1, ax2]
#First version of plots - like a shadow of every elements
for ax in axes:
ax.plot(financial_crisis['Time'], financial_crisis['rolling_mean'],
color='#af0b1e', alpha=0.1)
ax.set_ylim(1.12, 1.6) #use same y axis for both graph
#ax.set_yticks([1.0, 1.2,1.4, 1.6])
ax.set_yticklabels([])
#ax.set_facecolor="#dbd7d7"
ax.tick_params(colors='#948484', which='both', labelsize=10)
ax.grid(b=True, color="grey", alpha=0.1,linestyle=":")
### Highlihting the rate growing before crises
ax1.plot(financial_crisis['Time'][:660], financial_crisis['rolling_mean'][:660], color='green', linewidth=2.55, alpha=0.8)
#ax1.xaxis.tick_top()
ax1.set_xticklabels([])
ax1.text(x=13000,y=1.12,s="r*=1.1826",size=10) #check appendix
ax1.text(x=14000,y=1.6,s="r*=1.599",size=10) #check appendix
ax1.text(x=14250,y=1.65,s="Positive rate evolution",size=11,color="green", weight="bold")
ax1.text(x=14250,y=1.55,s="until middle 2008",size=11, color="green", weight="bold")
### Highlihting the floated rate after crisis
ax2.plot((financial_crisis['Time'][660:1300]), (financial_crisis['rolling_mean'][660:1300]),color='#af0b1e', linewidth=2.5, alpha=0.8)
ax2.text(x=13030,y=1.45,s="The rate drops and",size=11, color="#af0b1e", weight="bold")
ax2.text(x=13030,y=1.35,s="grows after 2008",size=11, color="#af0b1e", weight="bold")
### Highlihting the peak of the crisis
ax1.axvspan(xmin=13950, xmax=14140, ymin=0.09,
alpha=0.3, color='grey')
ax2.axvspan(xmin=13950, xmax=14140, ymin=0.09,
alpha=0.3, color='grey')
ax2.text(x=14900,y=1.26,s="r*=1.1.2939",size=10) #check appendix
### Highlihting the moment of the dratic rate drop
#ax1.axvline(x=14090, ymin=-5, ymax=1.3, c="red",linestyle=":", linewidth=2)
#ax2.axvline(x=14090, ymin=-3, ymax=2, c="red", linewidth=2,linestyle=":")
#Title and subtitle:
ax1.text(x=13000,y=2.05,s="EUR/USD rate drastic drop after the financial crisis",size=18, weight="bold")
ax1.text(x=13200,y=1.90,s="The global financial crisis has a big impact on the exchange rate. ",size=13)
ax1.text(x=13000,y=1.80,s="The rate started to float after 2008 and never reach again its highest value 1.599",size=13)
#r description
ax.text(x=13000,y=0.9, s="*r means exchange rate EUR/USD", size=11)
#Foot signature/source of graph:
ax2.text(13000,0.8,'©Mariana Lourenço' +' '*59 +"Source: European Central Bank", color="#f0f0f0",backgroundcolor="#4d4d4d", size=12)
plt.show()
#2016-2019:
before_pandemic = euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2016) & (euro_to_dollar['Time'].dt.year <= 2019)]
#2020:
after_pandemic =euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2020) & (euro_to_dollar['Time'].dt.year <= 2021)]
#import matplotlib.dates as mdates
fig, ax= plt.subplots(figsize=(9,1.5))
ax.plot(before_pandemic['Time'], before_pandemic['rolling_mean'],color='lightblue')
ax.plot(after_pandemic['Time'], after_pandemic['rolling_mean'],color='green' )
#ax.set_ylim(1.1, 1.3) #use same y axis for both graph
#ax.set_yticks([1.1, 1.2,1.3])
#ax.set_yticklabels([1.1, 1.2,1.3])
ax.tick_params(colors='#948484', which='both', labelsize=12)
ax.yaxis.grid(False)
ax.set_yticklabels([])
ax.axhline(1.22,c="black", linewidth=2.5, alpha=0.8)
ax.text(x=16530,y=1.21, s="r*=1.225", size=11)
ax.annotate('Mar-2020', xy=(18400, 1.08), xytext=(18500, 0.95),
arrowprops=dict(facecolor='black', shrink=0.05),
)
#Title
ax.text(x=16600,y=1.45,s="COVID-19 Pandemic rises hopes that the euro will valorize ",size=18, weight="bold")
ax.text(x=16750,y=1.40, s="Checking evolution of EUR/USD rate, since first cases in Europe (Mar/2020)",size=14)
ax.text(x=16800,y=1.35, s=" the rate grew, and in Jan-2021 reached the highest values since 2018",size=14)
#r description
ax.text(x=16530,y=0.92, s="*r means exchange rate EUR/USD", size=11)
#Foot signature/source of graph:
ax.text(16530,0.88,'©Mariana Lourenço' +' '*78 +"Source: European Central Bank", backgroundcolor="#4d4d4d", size=12,color="#f0f0f0")
plt.show()
before=euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2006) & (euro_to_dollar['Time'].dt.year <= 2008)]
print(before.shape)
before.max()
before[before["rolling_mean"]== 1.5743]
before.min()
before[before["Time"]== "2006-01-02"]
euro_to_dollar[euro_to_dollar["Time"]=="2011-12-30"]
pandemic=euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year >= 2020) & (euro_to_dollar['Time'].dt.year <= 2021)]
print(before.shape)
pandemic.min()
pandemic[pandemic["rolling_mean"]< 1.085]
pandemic[pandemic["US_dollar"]== 1.0707]
pandemic[pandemic["Time"]== "2021-01-08"]