In [1]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

exchange_rates = pd.read_csv('euro-daily-hist_1999_2020.csv')
print(exchange_rates.head())
print(exchange_rates.tail())
exchange_rates.info()
  Period\Unit: [Australian dollar ] [Bulgarian lev ] [Brazilian real ]  \
0   2021-01-08               1.5758           1.9558            6.5748   
1   2021-01-07               1.5836           1.9558            6.5172   
2   2021-01-06               1.5824           1.9558            6.5119   
3   2021-01-05               1.5927           1.9558            6.5517   
4   2021-01-04               1.5928           1.9558            6.3241   

  [Canadian dollar ] [Swiss franc ] [Chinese yuan renminbi ] [Cypriot pound ]  \
0             1.5543         1.0827                   7.9184              NaN   
1             1.5601         1.0833                   7.9392              NaN   
2             1.5640         1.0821                   7.9653              NaN   
3             1.5651         1.0803                   7.9315              NaN   
4             1.5621         1.0811                   7.9484              NaN   

  [Czech koruna ] [Danish krone ]  ... [Romanian leu ] [Russian rouble ]  \
0          26.163          7.4369  ...          4.8708           90.8000   
1          26.147          7.4392  ...          4.8712           91.2000   
2          26.145          7.4393  ...          4.8720           90.8175   
3          26.227          7.4387  ...          4.8721           91.6715   
4          26.141          7.4379  ...          4.8713           90.3420   

  [Swedish krona ] [Singapore dollar ] [Slovenian tolar ] [Slovak koruna ]  \
0          10.0510              1.6228                NaN              NaN   
1          10.0575              1.6253                NaN              NaN   
2          10.0653              1.6246                NaN              NaN   
3          10.0570              1.6180                NaN              NaN   
4          10.0895              1.6198                NaN              NaN   

  [Thai baht ] [Turkish lira ] [US dollar ]  [South African rand ]  
0      36.8480          9.0146       1.2250                18.7212  
1      36.8590          8.9987       1.2276                18.7919  
2      36.9210          9.0554       1.2338                18.5123  
3      36.7760          9.0694       1.2271                18.4194  
4      36.7280          9.0579       1.2296                17.9214  

[5 rows x 41 columns]
     Period\Unit: [Australian dollar ] [Bulgarian lev ] [Brazilian real ]  \
5694   1999-01-08               1.8406              NaN               NaN   
5695   1999-01-07               1.8474              NaN               NaN   
5696   1999-01-06               1.8820              NaN               NaN   
5697   1999-01-05               1.8944              NaN               NaN   
5698   1999-01-04               1.9100              NaN               NaN   

     [Canadian dollar ] [Swiss franc ] [Chinese yuan renminbi ]  \
5694             1.7643         1.6138                      NaN   
5695             1.7602         1.6165                      NaN   
5696             1.7711         1.6116                      NaN   
5697             1.7965         1.6123                      NaN   
5698             1.8004         1.6168                      NaN   

     [Cypriot pound ] [Czech koruna ] [Danish krone ]  ... [Romanian leu ]  \
5694          0.58187          34.938          7.4433  ...          1.3143   
5695          0.58187          34.886          7.4431  ...          1.3092   
5696          0.58200          34.850          7.4452  ...          1.3168   
5697          0.58230          34.917          7.4495  ...          1.3168   
5698          0.58231          35.107          7.4501  ...          1.3111   

     [Russian rouble ] [Swedish krona ] [Singapore dollar ]  \
5694           27.2075           9.1650              1.9537   
5695           26.9876           9.1800              1.9436   
5696           27.4315           9.3050              1.9699   
5697           26.5876           9.4025              1.9655   
5698           25.2875           9.4696              1.9554   

     [Slovenian tolar ] [Slovak koruna ] [Thai baht ] [Turkish lira ]  \
5694           188.8400           42.560      42.5590          0.3718   
5695           188.8000           42.765      42.1678          0.3701   
5696           188.7000           42.778      42.6949          0.3722   
5697           188.7750           42.848      42.5048          0.3728   
5698           189.0450           42.991      42.6799          0.3723   

     [US dollar ]  [South African rand ]  
5694       1.1659                 6.7855  
5695       1.1632                 6.8283  
5696       1.1743                 6.7307  
5697       1.1790                 6.7975  
5698       1.1789                 6.9358  

[5 rows x 41 columns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5699 entries, 0 to 5698
Data columns (total 41 columns):
 #   Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   Period\Unit:              5699 non-null   object 
 1   [Australian dollar ]      5699 non-null   object 
 2   [Bulgarian lev ]          5297 non-null   object 
 3   [Brazilian real ]         5431 non-null   object 
 4   [Canadian dollar ]        5699 non-null   object 
 5   [Swiss franc ]            5699 non-null   object 
 6   [Chinese yuan renminbi ]  5431 non-null   object 
 7   [Cypriot pound ]          2346 non-null   object 
 8   [Czech koruna ]           5699 non-null   object 
 9   [Danish krone ]           5699 non-null   object 
 10  [Estonian kroon ]         3130 non-null   object 
 11  [UK pound sterling ]      5699 non-null   object 
 12  [Greek drachma ]          520 non-null    object 
 13  [Hong Kong dollar ]       5699 non-null   object 
 14  [Croatian kuna ]          5431 non-null   object 
 15  [Hungarian forint ]       5699 non-null   object 
 16  [Indonesian rupiah ]      5699 non-null   object 
 17  [Israeli shekel ]         5431 non-null   object 
 18  [Indian rupee ]           5431 non-null   object 
 19  [Iceland krona ]          3292 non-null   float64
 20  [Japanese yen ]           5699 non-null   object 
 21  [Korean won ]             5699 non-null   object 
 22  [Lithuanian litas ]       4159 non-null   object 
 23  [Latvian lats ]           3904 non-null   object 
 24  [Maltese lira ]           2346 non-null   object 
 25  [Mexican peso ]           5699 non-null   object 
 26  [Malaysian ringgit ]      5699 non-null   object 
 27  [Norwegian krone ]        5699 non-null   object 
 28  [New Zealand dollar ]     5699 non-null   object 
 29  [Philippine peso ]        5699 non-null   object 
 30  [Polish zloty ]           5699 non-null   object 
 31  [Romanian leu ]           5637 non-null   float64
 32  [Russian rouble ]         5699 non-null   object 
 33  [Swedish krona ]          5699 non-null   object 
 34  [Singapore dollar ]       5699 non-null   object 
 35  [Slovenian tolar ]        2085 non-null   object 
 36  [Slovak koruna ]          2608 non-null   object 
 37  [Thai baht ]              5699 non-null   object 
 38  [Turkish lira ]           5637 non-null   float64
 39  [US dollar ]              5699 non-null   object 
 40  [South African rand ]     5699 non-null   object 
dtypes: float64(3), object(38)
memory usage: 1.8+ MB
In [2]:
exchange_rates.rename(columns={'[US dollar ]': 'US_dollar',
                               'Period\\Unit:': 'Time'},
                      inplace=True)
exchange_rates['Time'] = pd.to_datetime(exchange_rates['Time'])
exchange_rates.sort_values('Time', inplace=True)
exchange_rates.reset_index(drop=True, inplace=True)
euro_to_dollar = exchange_rates[['Time', 'US_dollar']]
print(euro_to_dollar)
print(euro_to_dollar['US_dollar'].value_counts())
euro_to_dollar = euro_to_dollar.loc[euro_to_dollar['US_dollar'] != '-', :]
euro_to_dollar['US_dollar'] = euro_to_dollar['US_dollar'].astype('float64')
print(euro_to_dollar['US_dollar'].value_counts())
print(euro_to_dollar)
euro_to_dollar.info()
           Time US_dollar
0    1999-01-04    1.1789
1    1999-01-05    1.1790
2    1999-01-06    1.1743
3    1999-01-07    1.1632
4    1999-01-08    1.1659
...         ...       ...
5694 2021-01-04    1.2296
5695 2021-01-05    1.2271
5696 2021-01-06    1.2338
5697 2021-01-07    1.2276
5698 2021-01-08    1.2250

[5699 rows x 2 columns]
-         62
1.2276     9
1.1215     8
1.1305     7
1.3532     6
          ..
1.3951     1
0.9327     1
0.9415     1
1.1821     1
1.1033     1
Name: US_dollar, Length: 3528, dtype: int64
1.2276    9
1.1215    8
1.1305    7
1.1268    6
1.3373    6
         ..
1.4639    1
1.5206    1
1.3272    1
0.8591    1
0.9375    1
Name: US_dollar, Length: 3527, dtype: int64
           Time  US_dollar
0    1999-01-04     1.1789
1    1999-01-05     1.1790
2    1999-01-06     1.1743
3    1999-01-07     1.1632
4    1999-01-08     1.1659
...         ...        ...
5694 2021-01-04     1.2296
5695 2021-01-05     1.2271
5696 2021-01-06     1.2338
5697 2021-01-07     1.2276
5698 2021-01-08     1.2250

[5637 rows x 2 columns]
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5637 entries, 0 to 5698
Data columns (total 2 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   Time       5637 non-null   datetime64[ns]
 1   US_dollar  5637 non-null   float64       
dtypes: datetime64[ns](1), float64(1)
memory usage: 132.1 KB
In [6]:
euro_to_dollar['rolling_mean'] = euro_to_dollar['US_dollar'].rolling(30).mean()
euro_to_dollar
Out[6]:
Time US_dollar rolling_mean
0 1999-01-04 1.1789 NaN
1 1999-01-05 1.1790 NaN
2 1999-01-06 1.1743 NaN
3 1999-01-07 1.1632 NaN
4 1999-01-08 1.1659 NaN
... ... ... ...
5694 2021-01-04 1.2296 1.211170
5695 2021-01-05 1.2271 1.212530
5696 2021-01-06 1.2338 1.213987
5697 2021-01-07 1.2276 1.215357
5698 2021-01-08 1.2250 1.216557

5637 rows × 3 columns

Show how September 11 2001 attacks affected the euro-to-dollar exchange rate, comparing say the year before the attack with the year after.

In [7]:
print(euro_to_dollar.iloc[0:15,])
fig, ax = plt.subplots(ncols=1, figsize=(8, 4))
ax.plot(euro_to_dollar['Time'], euro_to_dollar['rolling_mean'])
plt.show()
         Time  US_dollar  rolling_mean
0  1999-01-04     1.1789           NaN
1  1999-01-05     1.1790           NaN
2  1999-01-06     1.1743           NaN
3  1999-01-07     1.1632           NaN
4  1999-01-08     1.1659           NaN
5  1999-01-11     1.1569           NaN
6  1999-01-12     1.1520           NaN
7  1999-01-13     1.1744           NaN
8  1999-01-14     1.1653           NaN
9  1999-01-15     1.1626           NaN
10 1999-01-18     1.1612           NaN
11 1999-01-19     1.1616           NaN
12 1999-01-20     1.1575           NaN
13 1999-01-21     1.1572           NaN
14 1999-01-22     1.1567           NaN
In [12]:
import datetime
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
import matplotlib.style as style
style.use('fivethirtyeight')

### Adding the plot
fig,ax = plt.subplots(figsize=(8,3))
ax.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar'], linewidth = 0.7)
# ax.set_xticks([datetime.date(2000, 9, 11), datetime.date(2001, 9, 11), datetime.date(2002, 9, 11), datetime.date(2003, 9, 10)])

# date_list.append(datetime.date(2001, 9, 11))
# ax.set_xticks(datetime.date(2001, 9, 11))
# ax.set_xticklabels(datetime.date(2001, 9, 11))
# ax.set_xticks([datetime.date(2001, 9, 10), datetime.date(2001, 9, 11)])
# ax.set_xticklabels([datetime.date(2000, 9, 11), datetime.date(2001, 9, 11), datetime.date(2002, 9, 11), datetime.date(2003, 9, 10)])
# ax.set_xticklabels([datetime.date(2001, 9, 10), datetime.date(2001, 9, 11)])
# ax.set_xlim([datetime.date(2001, 9, 1), datetime.date(2001, 9, 30)])
ax.set_xlim([datetime.date(2000, 9, 1), datetime.date(2002, 9, 19)])
ax.set_ylim(0.75, 1.05)
# ax.xaxis.set_major_locator(mdates.MonthLocator(interval = 1))
date_list = []
for i in [8, 10]:
        date_list.append(datetime.date(2001, i, 11))
for j in [2000, 2002]:
        date_list.append(datetime.date(j, 9 , 11))
y_list = [0.8, 0.9, 1.0]
fig.autofmt_xdate()
ax.set_xticks(date_list)
ax.set_xticklabels(date_list, size = 10, rotation = 45, rotation_mode = 'anchor')
ax.set_yticks(y_list)
ax.set_yticklabels(y_list, size = 10)
ax.axvline(datetime.date(2001, 9, 11), linewidth = 0.6, color = 'red', linestyle = 'solid')
ax.text(datetime.date(2001, 9, 11), 0.75,'2001-09-11', color = 'red', size = 12, rotation = 45,
          rotation_mode = 'anchor', ha='right', va = 'top')
ax.set_ylabel('exchange rate', size = 10)
plt.title('Euro dollar exchange rate around September 11th 2001', size = 14)
plt.show()
# time_range = euro_to_dollar.copy(
#                    )[(euro_to_dollar['Time'].dt.date >= 2000/9/11
#                    ) & (euro_to_dollar['Time'].dt.date <= 2003/9/10)]
# year_after = euro_to_dollar.copy(
#                    )[(euro_to_dollar.Time.dt.date >= 2001/9/11
#                    ) & (euro_to_dollar.Time.dt.date <= 2002/9/10)]