pick month start and end data in python
Question: I have stock data downloaded from yahoo finance. I want to pickup data in the
row corresponding to monthly start and month end. I am trying to do it with
python pandas data frame. But I am not getting correct method to get the
starting & ending of the month. will be great full if somebody can help me in
solving this. Please note that if 1st of the month is holiday and there is no
data for that, I need to pick up 2nd day's data. Same rule applies to last of
the month also. Thanks in advance.

Example data is

    
    
    2016-01-05,222.80,222.80,217.00,217.75,15074800,217.75
    2016-01-04,226.95,226.95,220.05,220.70,14092000,220.70
    2015-12-31,225.95,226.55,224.00,224.45,11558300,224.45
    2015-12-30,229.00,229.70,224.85,225.80,11702800,225.80
    2015-12-29,228.85,229.95,227.50,228.20,7263200,228.20
    2015-12-28,229.05,229.95,228.00,228.90,8756800,228.90
    ........
    ........
    2015-12-04,240.00,242.15,238.05,241.10,11115100,241.10
    2015-12-03,244.15,244.50,240.40,241.10,7155600,241.10
    2015-12-02,250.55,250.65,243.75,244.60,10881700,244.60
    2015-11-30,249.65,253.00,245.00,250.20,12865400,250.20
    2015-11-27,243.00,250.50,242.80,249.70,15149900,249.70
    2015-11-26,241.95,244.90,241.00,242.50,13629800,242.50
    


Answer: For the first / last day of each month, you can use `.resample()` with `'BMS'`
and `'BM'` for `Business Month (Start)` like so (using `pandas 0.18` syntax):

    
    
    df.resample('BMS').first()
    df.resample('BM').last()
    

This assumes that your data have a `DateTimeIndex` as usual when downloaded
from `yahoo` using `pandas_datareader`:

    
    
    from datetime import datetime
    from pandas_datareader.data import DataReader
    df = DataReader('FB', 'yahoo', datetime(2015, 1, 1), datetime(2015, 3, 31))['Open']
    
    df.head()
    
    Date
    2015-01-02    78.580002
    2015-01-05    77.980003
    2015-01-06    77.230003
    2015-01-07    76.760002
    2015-01-08    76.739998
    Name: Open, dtype: float64
    df.tail()
    
    Date
    2015-03-25    85.500000
    2015-03-26    82.720001
    2015-03-27    83.379997
    2015-03-30    83.809998
    2015-03-31    82.900002
    Name: Open, dtype: float64
    

do:

    
    
    df.resample('BMS').first()
    
    Date
    2015-01-01    78.580002
    2015-02-02    76.110001
    2015-03-02    79.000000
    Freq: BMS, Name: Open, dtype: float64
    

and

    
    
    df.resample('BM').last()
    

to get:

    
    
    Date
    2015-01-30    78.000000
    2015-02-27    80.680000
    2015-03-31    82.900002
    Freq: BM, Name: Open, dtype: float64
    

