Applying Python to Finance


In [2]:
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
%matplotlib inline

Can use different plot styles that have already been defined


In [3]:
style.use('ggplot')

In [4]:
start = dt.datetime(2000,1,1)
end = dt.datetime(2016,12,31)

Using pandas datareader

pandas datareader replaced pandas.io.data and is a standalone install


In [5]:
df = web.DataReader('TSLA', 'yahoo', start, end)

In [6]:
df


Out[6]:
Open High Low Close Volume Adj Close
Date
2010-06-29 19.000000 25.000000 17.540001 23.889999 18766300 23.889999
2010-06-30 25.790001 30.420000 23.299999 23.830000 17187100 23.830000
2010-07-01 25.000000 25.920000 20.270000 21.959999 8218800 21.959999
2010-07-02 23.000000 23.100000 18.709999 19.200001 5139800 19.200001
2010-07-06 20.000000 20.000000 15.830000 16.110001 6866900 16.110001
2010-07-07 16.400000 16.629999 14.980000 15.800000 6921700 15.800000
2010-07-08 16.139999 17.520000 15.570000 17.459999 7711400 17.459999
2010-07-09 17.580000 17.900000 16.549999 17.400000 4050600 17.400000
2010-07-12 17.950001 18.070000 17.000000 17.049999 2202500 17.049999
2010-07-13 17.389999 18.639999 16.900000 18.139999 2680100 18.139999
2010-07-14 17.940001 20.150000 17.760000 19.840000 4195200 19.840000
2010-07-15 19.940001 21.500000 19.000000 19.889999 3739800 19.889999
2010-07-16 20.700001 21.299999 20.049999 20.639999 2621300 20.639999
2010-07-19 21.370001 22.250000 20.920000 21.910000 2486500 21.910000
2010-07-20 21.850000 21.850000 20.049999 20.299999 1825300 20.299999
2010-07-21 20.660000 20.900000 19.500000 20.219999 1252500 20.219999
2010-07-22 20.500000 21.250000 20.370001 21.000000 957800 21.000000
2010-07-23 21.190001 21.559999 21.059999 21.290001 653600 21.290001
2010-07-26 21.500000 21.500000 20.299999 20.950001 922200 20.950001
2010-07-27 20.910000 21.180000 20.260000 20.549999 619700 20.549999
2010-07-28 20.549999 20.900000 20.510000 20.719999 467200 20.719999
2010-07-29 20.770000 20.879999 20.000000 20.350000 616000 20.350000
2010-07-30 20.200001 20.440001 19.549999 19.940001 426900 19.940001
2010-08-02 20.500000 20.969999 20.330000 20.920000 718100 20.920000
2010-08-03 21.000000 21.950001 20.820000 21.950001 1230500 21.950001
2010-08-04 21.950001 22.180000 20.850000 21.260000 913000 21.260000
2010-08-05 21.540001 21.549999 20.049999 20.450001 796200 20.450001
2010-08-06 20.100000 20.160000 19.520000 19.590000 741900 19.590000
2010-08-09 19.900000 19.980000 19.450001 19.600000 812700 19.600000
2010-08-10 19.650000 19.650000 18.820000 19.030001 1281300 19.030001
... ... ... ... ... ... ...
2016-11-17 183.490005 189.490005 182.110001 188.660004 4777800 188.660004
2016-11-18 190.649994 193.000000 185.000000 185.020004 5201100 185.020004
2016-11-21 185.039993 188.889999 184.410004 184.520004 4344600 184.520004
2016-11-22 185.839996 191.470001 183.710007 191.169998 5600300 191.169998
2016-11-23 190.610001 195.639999 189.000000 193.139999 4885300 193.139999
2016-11-25 193.639999 197.240005 193.639999 196.649994 2363800 196.649994
2016-11-28 195.479996 199.350006 194.550003 196.119995 4487100 196.119995
2016-11-29 195.559998 196.729996 189.500000 189.570007 4431200 189.570007
2016-11-30 191.000000 191.889999 187.500000 189.399994 3535000 189.399994
2016-12-01 188.250000 188.529999 181.000000 181.880005 5112100 181.880005
2016-12-02 182.880005 184.880005 180.000000 181.470001 4037200 181.470001
2016-12-05 182.509995 188.889999 182.509995 186.800003 4066500 186.800003
2016-12-06 185.520004 186.580002 182.679993 185.850006 3372000 185.850006
2016-12-07 186.149994 193.399994 185.000000 193.149994 5441400 193.149994
2016-12-08 192.050003 192.500000 189.539993 192.289993 3187300 192.289993
2016-12-09 190.869995 193.839996 190.809998 192.179993 2719600 192.179993
2016-12-12 192.800003 194.419998 191.179993 192.429993 615800 192.429993
2016-12-13 193.179993 201.279999 193.000000 198.149994 6816100 198.149994
2016-12-14 198.740005 203.000000 196.759995 198.690002 4144600 198.690002
2016-12-15 198.410004 200.740005 197.389999 197.580002 3218200 197.580002
2016-12-16 198.080002 202.589996 197.600006 202.490005 3779800 202.490005
2016-12-19 202.490005 204.449997 199.839996 202.729996 3481500 202.729996
2016-12-20 203.050003 209.000000 202.500000 208.789993 4681400 208.789993
2016-12-21 208.449997 212.229996 207.410004 207.699997 5204800 207.699997
2016-12-22 208.220001 209.990005 206.500000 208.449997 3106900 208.449997
2016-12-23 208.000000 213.449997 207.710007 213.339996 4662900 213.339996
2016-12-27 214.880005 222.250000 214.419998 219.529999 5901400 219.529999
2016-12-28 221.529999 223.800003 217.199997 219.740005 3766900 219.740005
2016-12-29 218.559998 219.199997 214.119995 214.679993 4035900 214.679993
2016-12-30 216.300003 217.500000 211.679993 213.690002 4632700 213.690002

1640 rows × 6 columns

Convert df to csv file


In [7]:
df.to_csv('tsla.csv')

In [8]:
df = pd.read_csv('tsla.csv', parse_dates=True, index_col=0)

In [9]:
df.head()


Out[9]:
Open High Low Close Volume Adj Close
Date
2010-06-29 19.000000 25.00 17.540001 23.889999 18766300 23.889999
2010-06-30 25.790001 30.42 23.299999 23.830000 17187100 23.830000
2010-07-01 25.000000 25.92 20.270000 21.959999 8218800 21.959999
2010-07-02 23.000000 23.10 18.709999 19.200001 5139800 19.200001
2010-07-06 20.000000 20.00 15.830000 16.110001 6866900 16.110001

In [16]:
df['High'].plot()


Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3038d0a588>

Codeando GDL


In [11]:
data = pd.read_csv('masterfile.csv')

In [14]:
data.columns


Out[14]:
Index(['NOMBRE', 'PATERNO', 'MATERNO', 'MUNICIPIO', 'BIMESTRE 1', 'FECHA 1',
       'BIMESTRE 2', 'FECHA 2', 'BIMESTRE 3', 'FECHA 3', 'BIMESTRE 4',
       'FECHA 4', 'BIMESTRE 5', 'FECHA 5', 'MONTO TOTAL'],
      dtype='object')

In [23]:
data.head()


Out[23]:
NOMBRE PATERNO MATERNO MUNICIPIO BIMESTRE 1 FECHA 1 BIMESTRE 2 FECHA 2 BIMESTRE 3 FECHA 3 BIMESTRE 4 FECHA 4 BIMESTRE 5 FECHA 5 MONTO TOTAL
0 HUMMBERTO ARTURO OCHOA HERNANDEZ ACATIC 2191.2 2016-03-18 2191.2 2016-05-04 2191.2 2016-07-04 2191.2 2016-09-05 2191.2 2016-11-09 10956.0
1 MA GUADALUPE BALLADARES ALVARES ACATIC 2191.2 2016-03-18 2191.2 2016-05-04 2191.2 2016-07-04 2191.2 2016-09-05 2191.2 2016-11-09 10956.0
2 MA ANGELINA VALLADARES ALVAREZ ACATIC 2191.2 2016-03-18 2191.2 2016-05-04 2191.2 2016-07-04 2191.2 2016-09-05 2191.2 2016-11-09 10956.0
3 MARIA MENDOZA HERNANDEZ ACATIC 2191.2 2016-03-18 2191.2 2016-05-04 2191.2 2016-07-04 2191.2 2016-09-05 2191.2 2016-11-09 10956.0
4 REFUJIO BARRIOS DE LEON ACATIC 2191.2 2016-03-18 NaN NaN NaN NaN NaN NaN NaN NaN 2191.2

In [ ]:


In [22]:
data['BIMESTRE 2'][:100].plot()


Out[22]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3ede191898>

In [ ]: