Conversion between time series datatypes

We can convert the Period and PeriodIndex datatypes to the Datetime/Timestamp and DatetimeIndex datatypes through the to_period and to_timestamp functions, as follows:

    In [339]: worldCupFinal=pd.to_datetime('07/13/2014', 
                                           errors='raise')
             worldCupFinal
       Out[339]: Timestamp('2014-07-13 00:00:00')
    
    In [340]: worldCupFinal.to_period('D')
       Out[340]: Period('2014-07-13', 'D')
    
    In [342]: worldCupKickoff=pd.Period('06/12/2014','D')
             worldCupKickoff
    Out[342]: Period('2014-06-12', 'D')
    In [345]: worldCupKickoff.to_timestamp()
    Out[345]: Timestamp('2014-06-12 00:00:00', tz=None)
    
    In [346]: worldCupDays=pd.date_range('06/12/2014',periods=32,   
                                          freq='D')
             worldCupDays
    Out[346]: <class 'pandas.tseries.index.DatetimeIndex'>
        [2014-06-12, ..., 2014-07-13]
        Length: 32, Freq: D, Timezone: None
    
    In [347]: worldCupDays.to_period()
    Out[347]: <class 'pandas.tseries.period.PeriodIndex'>
        freq: D
        [2014-06-12, ..., 2014-07-13]
        length: 32
  

In the preceding examples, note how periods are converted into timestamps and vice versa.

..................Content has been hidden....................

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