Datetime Index

Read data

import pandas as pd
df = pd.read_csv("apple_stock_price.csv",parse_dates=["Date"], index_col="Date")
df.head(2)
Out[15]:
     
            Open	      High	     Low	         Close	    Adj Close	  Volume
Date						
2018-07-23	190.679993	191.960007	189.559998	191.610001	188.737030	15989400
2018-07-24	192.449997	193.660004	192.050003	193.000000	190.106216	18697900

What is DatetimeIndex? Benefits of it

  1. Partial Date Index: Select Specific Months Data

df["2019-07"]
Out[21]:
                Open	   High	      Low	        Close	     Adj Close	  Volume
Date						
2019-07-01	203.169998	204.490005	200.649994	201.550003	201.550003	27316700
2019-07-02	201.410004	203.130005	201.360001	202.729996	202.729996	16935200
2019-07-03	203.279999	204.440002	202.690002	204.410004	204.410004	11362000
2019-07-05	203.350006	205.080002	202.899994	204.229996	204.229996	17265500
2019-07-08	200.809998	201.399994	198.410004	200.020004	200.020004	25338600
2019-07-09	199.199997	201.509995	198.809998	201.240005	201.240005	20578000
2019-07-10	201.850006	203.729996	201.559998	203.229996	203.229996	17897100
2019-07-11	203.309998	204.389999	201.710007	201.750000	201.750000	20191800
2019-07-12	202.449997	204.000000	202.199997	203.300003	203.300003	17595200
2019-07-15	204.089996	205.869995	204.000000	205.210007	205.210007	16947400
2019-07-16	204.589996	206.110001	203.500000	204.500000	204.500000	16866800
2019-07-17	204.050003	205.089996	203.270004	203.350006	203.350006	14107500
2019-07-18	204.000000	205.880005	203.699997	205.660004	205.660004	18582200
2019-07-19	205.789993	206.500000	202.360001	202.589996	202.589996	20929300
2019-07-22	203.649994	207.229996	203.610001	207.220001	207.220001	22241300
2019-07-23	208.460007	208.789993	207.440002	208.126907	208.126907	5728710

2.Average price of apple's stock in June, 2019

df['2019-06'].Close.mean()

Out[26]:
192.96900015
df['2019'].head(2)
Out[27]:
                Open	   High	       Low	      Close	     Adj Close	  Volume
Date						
2019-01-02	154.889999	158.850006	154.229996	157.919998	156.642365	37039700
2019-01-03	143.979996	145.720001	142.000000	142.190002	141.039642	91312200

Last updated