Open In Colab

18: Pandas and Timeseries#

1. Working with Timeseries#

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
bitcoin = pd.read_csv('../data/BTC-EUR.csv', index_col='Date', parse_dates=True)
bitcoin.head()
Open High Low Close Adj Close Volume
Date
2011-10-04 3.700 3.821 3.746 3.750 3.750 1357
2011-10-05 3.750 3.820 3.650 3.676 3.676 3349
2011-10-06 3.676 3.743 3.450 3.550 3.550 6642
2011-10-07 3.550 3.590 2.900 3.293 3.293 7135
2011-10-08 3.293 3.283 2.872 2.890 2.890 2007
bitcoin['Close'].plot(figsize=(9, 6))
plt.show()
../../../_images/1a4e0621f658a699e391a643855bbb21d50f937681aa1530f14d9d0fef0a2053.png
bitcoin.index
DatetimeIndex(['2011-10-04', '2011-10-05', '2011-10-06', '2011-10-07',
               '2011-10-08', '2011-10-09', '2011-10-10', '2011-10-11',
               '2011-10-12', '2011-10-13',
               ...
               '2019-09-25', '2019-09-26', '2019-09-27', '2019-09-28',
               '2019-09-29', '2019-09-30', '2019-10-01', '2019-10-02',
               '2019-10-03', '2019-10-04'],
              dtype='datetime64[ns]', name='Date', length=2923, freq=None)
bitcoin.loc['2017':'2019','Close'].plot()
<AxesSubplot:xlabel='Date'>
../../../_images/6f01b7a430a99b9e7f19ef02dd75f79b71cada06deb80eea86b3c9a8009d6aa0.png

2. Resample#

bitcoin.loc['2019', 'Close'].resample('M').plot()
plt.show()
../../../_images/b9d04e73c1bfec93e895d80445c8f18ebfff7f20a501673e67d9fa64327ed8f6.png
bitcoin.loc['2019', 'Close'].resample('2W').mean().plot()
plt.show()
../../../_images/4dda1186142d47507745c95359e3c7a6ec33cdc07200d9181d38ce4f19179564.png
bitcoin.loc['2019', 'Close'].resample('2W').std().plot()
plt.show()
../../../_images/b128062e3f6c8d0557be261e21f62bb232cc9c0cbb3d31c973dffd5a8489ee91.png
plt.figure(figsize=(12, 8))
bitcoin.loc['2019', 'Close'].plot()
bitcoin.loc['2019', 'Close'].resample('M').mean().plot(label='moyenne par mois', lw=3, ls=':', alpha=0.8)
bitcoin.loc['2019', 'Close'].resample('W').mean().plot(label='moyenne par semaine', lw=2, ls='--', alpha=0.8)
plt.legend()
plt.show()
../../../_images/9b9a6352b8851dc19c8be3cdb39b7d2c00b334bebabf440013b84e53d5b19359.png

3. Aggregate#

m = bitcoin['Close'].resample('W').agg(['mean', 'std', 'min', 'max'])

plt.figure(figsize=(12, 8))
m['mean']['2019'].plot(label='moyenne par semaine')
plt.fill_between(m.index, m['max'], m['min'], alpha=0.2, label='min-max par semaine')

plt.legend()
plt.show()
../../../_images/210b1a370286474fce7b1c164616bbab2940242a77e0968069edd077435198bf.png
bitcoin.loc['2019', 'Close'].resample('W').agg(['mean', 'std', 'min', 'max']).plot()
<AxesSubplot:xlabel='Date'>
../../../_images/a1e72fea103e3948a0d37856fa54d49cfe37722ba5998b2b2733d83823bffa85.png

4. Moving Average et EWM#

plt.figure(figsize=(12, 8))
bitcoin.loc['2019-09', 'Close'].plot()
bitcoin.loc['2019-09', 'Close'].rolling(window=7).mean().plot(label='non centre', lw=3, ls=':', alpha=0.8)
bitcoin.loc['2019-09', 'Close'].rolling(window=7, center=True).mean().plot(label='centre', lw=3, ls=':', alpha=0.8)
bitcoin.loc['2019-09', 'Close'].ewm(alpha=0.6).mean().plot(label='ewm', lw=3, ls=':', alpha=0.8)
plt.legend()
plt.show()
../../../_images/444ff1d524ffce214cd2c12b65509f2ce641a6438bb77b1ee63cbe3e47a96075.png
plt.figure(figsize=(12, 8))
bitcoin.loc['2019-09', 'Close'].plot()
for i in np.arange(0.2, 1, 0.2):
    bitcoin.loc['2019-09', 'Close'].ewm(alpha=i).mean().plot(label=f'ewm {i}', ls='--', alpha=0.8)
plt.legend()
plt.show()
../../../_images/a13aafb6e52d31b10fce7d7cd3dd882722fa07f96821081212fe51031a80fed0.png

5. Comparaison of 2 timeseries#

ethereum = pd.read_csv('../data/ETH-EUR.csv', index_col='Date', parse_dates=True)
btc_eth = pd.merge(bitcoin, ethereum, on='Date', how='inner', suffixes=('_btc', '_eth'))
btc_eth[['Close_btc', 'Close_eth']]['2019-09'].plot(subplots=True, figsize=(12, 8))
/tmp/ipykernel_3878297/944741524.py:1: FutureWarning: Indexing a DataFrame with a datetimelike index using a single string to slice the rows, like `frame[string]`, is deprecated and will be removed in a future version. Use `frame.loc[string]` instead.
  btc_eth[['Close_btc', 'Close_eth']]['2019-09'].plot(subplots=True, figsize=(12, 8))
array([<AxesSubplot:xlabel='Date'>, <AxesSubplot:xlabel='Date'>],
      dtype=object)
../../../_images/63d28e7435466b454fcd0ad6b0b30f2d561a02487a718ebea36760e15bccdc07.png

6. Exercice and Solution#

data = bitcoin.copy()
data['Buy'] = np.zeros(len(data))
data['Sell'] = np.zeros(len(data))
data['RollingMax'] = data['Close'].shift(1).rolling(window=28).max()
data['RollingMin'] = data['Close'].shift(1).rolling(window=28).min()
data.loc[data['RollingMax'] < data['Close'], 'Buy'] = 1
data.loc[data['RollingMin'] > data['Close'], 'Sell'] = -1

Solution#

Hide code cell content
start ='2019'
end='2019'
fig, ax = plt.subplots(2, figsize=(12, 8), sharex=True)
#plt.figure(figsize=(12, 8))
#plt.subplot(211)
ax[0].plot(data['Close'][start:end])
ax[0].plot(data['RollingMin'][start:end])
ax[0].plot(data['RollingMax'][start:end])
ax[0].legend(['close', 'min', 'max'])
ax[1].plot(data['Buy'][start:end], c='g')
ax[1].plot(data['Sell'][start:end], c='r')
ax[1].legend(['buy', 'sell'])
<matplotlib.legend.Legend at 0x7f2660792ac0>
../../../_images/62c07db85d7757e7cdd03b3081582af21f4f521fff637af5600d98914a198709.png