Period and PeriodIndex

The Period datatype is used to represent a range or span of time. Here are a few examples:

    # Period representing May 2014
    In [287]: pd.Period('2014', freq='A-MAY')
    Out[287]: Period('2014', 'A-MAY')
    
    # Period representing specific day - June 11, 2014
    In [292]: pd.Period('06/11/2014')
    Out[292]: Period('2014-06-11', 'D')
    
    # Period representing 11AM, Nov 11, 1918 
    In [298]: pd.Period('11/11/1918 11:00',freq='H')
    Out[298]: Period('1918-11-11 11:00', 'H')

We can add integers to the Period datatypes to advance the period by the requisite number of frequency units:

    In [299]: pd.Period('06/30/2014')+4
    Out[299]: Period('2014-07-04', 'D')
    
    In [303]: pd.Period('11/11/1918 11:00',freq='H') - 48
    Out[303]: Period('1918-11-09 11:00', 'H')  

We can also calculate the difference between two the Period datatypes and return the number of units of frequency between them:

    In [304]: pd.Period('2014-04', freq='M')-pd.Period('2013-02', freq='M')
    Out[304]: 14
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset