Date Range

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

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

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

Select Date Range

df['2018-12-08':'2019-01-08']
Out[29]:
Open	High	Low	Close	Adj Close	Volume
Date						
2018-12-10	165.000000	170.089996	163.330002	169.600006	168.227890	62026000
2018-12-11	171.660004	171.789993	167.000000	168.630005	167.265732	47281700
2018-12-12	170.399994	171.919998	169.020004	169.100006	167.731934	35627700
2018-12-13	170.490005	172.570007	169.550003	170.949997	169.566956	31898600
2018-12-14	169.000000	169.080002	165.279999	165.479996	164.141220	40703700
2018-12-17	165.449997	168.350006	162.729996	163.940002	162.613678	44287900
2018-12-18	165.380005	167.529999	164.389999	166.070007	164.726440	33841500
2018-12-19	166.000000	167.449997	159.089996	160.889999	159.588348	49047300
2018-12-20	160.399994	162.110001	155.300003	156.830002	155.561188	64773000
2018-12-21	156.860001	158.160004	149.630005	150.729996	149.510544	95744600
2018-12-24	148.149994	151.550003	146.589996	146.830002	145.642090	37169200
2018-12-26	148.300003	157.229996	146.720001	157.169998	155.898438	58582500
2018-12-27	155.839996	156.770004	150.070007	156.149994	154.886688	53117100
2018-12-28	157.500000	158.520004	154.550003	156.229996	154.966034	42291400
2018-12-31	158.529999	159.360001	156.479996	157.740005	156.463837	35003500
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
2019-01-04	144.529999	148.550003	143.800003	148.259995	147.060516	58607100
2019-01-07	148.699997	148.830002	145.899994	147.929993	146.733185	54777800
2019-01-08	149.559998	151.820007	148.520004	150.750000	149.530380	41025300

Resampling

df['Close'].resample('M').mean().head()

Out[30]:
Date
2018-07-31    192.117144
2018-08-31    213.346089
2018-09-30    222.073685
2018-10-31    220.845652
2018-11-30    191.235714
Freq: M, Name: Close, dtype: float64

Draw plot with matplotlib

%matplotlib inline
df['Close'].plot()
Out[36]:
<matplotlib.axes._subplots.AxesSubplot at 0x1f0b45b7da0>
# By month
df['Close'].resample('M').mean().plot()

Last updated