Period
¶

In [2]:
import pandas as pd
import numpy as np

Period¶

Create a Period¶

In [3]:
# value, represents the entire period
x = pd.Period(value = '2014') # create a one-year period
In [4]:
q = pd.Period(value = '2014Q1') # create a one-quarter period
In [5]:
m = pd.Period(value = '2014-1', freq = 'D') # create a one-month period
In [6]:
d = pd.Period(value = '2014-1-1', freq = 'H') # create a one-day period
In [7]:
h = pd.Period(value = '2014-1-1, 23:00:00', freq = 'H') # create a one-hour period

Attributes¶

In [8]:
x.freq
Out[8]:
<YearEnd: month=12>
In [9]:
x.start_time
Out[9]:
Timestamp('2014-01-01 00:00:00')
In [10]:
x.end_time
Out[10]:
Timestamp('2014-12-31 23:59:59.999999999')
In [11]:
# convert frequency
x.asfreq('D', 'start')
Out[11]:
Period('2014-01-01', 'D')

Arithmetics¶

In [12]:
m+1 # unit depends on frequency
Out[12]:
Period('2014-01-02', 'D')
In [13]:
h + pd.offsets.Hour(2)
Out[13]:
Period('2014-01-02 01:00', 'H')
In [20]:
import datetime

h + datetime.timedelta(minutes=120)
Out[20]:
Period('2014-01-02 01:00', 'H')

Period Index¶

In [17]:
periods = [pd.Period("2012-01"), pd.Period("2012-02"), pd.Period("2012-03")]
ts = pd.Series(np.random.randn(3), periods) # Period to PeriodIndex
ts.index
Out[17]:
PeriodIndex(['2012-01', '2012-02', '2012-03'], dtype='period[M]')
In [28]:
idx = pd.period_range('2017', '2018', freq='Q') # PeriodIndex
idx
Out[28]:
PeriodIndex(['2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1'], dtype='period[Q-DEC]')
In [37]:
idx = pd.period_range('2017', periods = 10, freq='Q')
idx
Out[37]:
PeriodIndex(['2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1', '2018Q2',
             '2018Q3', '2018Q4', '2019Q1', '2019Q2'],
            dtype='period[Q-DEC]')
In [38]:
# change period data type and resample
idx.astype("period[D]")
Out[38]:
PeriodIndex(['2017-03-31', '2017-06-30', '2017-09-30', '2017-12-31',
             '2018-03-31', '2018-06-30', '2018-09-30', '2018-12-31',
             '2019-03-31', '2019-06-30'],
            dtype='period[D]')
In [62]:
idx.start_time
Out[62]:
DatetimeIndex(['2017-01-01', '2017-04-01', '2017-07-01', '2017-10-01',
               '2018-01-01'],
              dtype='datetime64[ns]', freq='QS-OCT')
In [64]:
idx.shape
Out[64]:
(5,)
In [65]:
idx[0] # Period
Out[65]:
Period('2017Q1', 'Q-DEC')
In [66]:
idx[0].start_time
Out[66]:
Timestamp('2017-01-01 00:00:00')

Series¶

In [72]:
s = pd.Series(range(len(idx)), index = idx)
s
Out[72]:
2017Q1    0
2017Q2    1
2017Q3    2
2017Q4    3
2018Q1    4
2018Q2    5
2018Q3    6
2018Q4    7
2019Q1    8
2019Q2    9
Freq: Q-DEC, dtype: int64
In [73]:
s['2017']
Out[73]:
2017Q1    0
2017Q2    1
2017Q3    2
2017Q4    3
Freq: Q-DEC, dtype: int64
In [74]:
s['2017':'2018']
Out[74]:
2017Q1    0
2017Q2    1
2017Q3    2
2017Q4    3
2018Q1    4
2018Q2    5
2018Q3    6
2018Q4    7
Freq: Q-DEC, dtype: int64
In [82]:
st = s.to_timestamp() # convert Period Index to Datetime Index
st, type(st.index), st.index[0]
Out[82]:
(2017-01-01    0
 2017-04-01    1
 2017-07-01    2
 2017-10-01    3
 2018-01-01    4
 2018-04-01    5
 2018-07-01    6
 2018-10-01    7
 2019-01-01    8
 2019-04-01    9
 Freq: QS-OCT, dtype: int64,
 pandas.core.indexes.datetimes.DatetimeIndex,
 Timestamp('2017-01-01 00:00:00', freq='QS-OCT'))
In [85]:
st.to_period() # convert DateTime Index to Period Index
Out[85]:
2017Q1    0
2017Q2    1
2017Q3    2
2017Q4    3
2018Q1    4
2018Q2    5
2018Q3    6
2018Q4    7
2019Q1    8
2019Q2    9
Freq: Q-DEC, dtype: int64

Conert to Datetime¶

In [47]:
# convert PeriodIndex to DatetimeIndex
idx.to_timestamp()
Out[47]:
DatetimeIndex(['2017-01-01', '2017-04-01', '2017-07-01', '2017-10-01',
               '2018-01-01', '2018-04-01', '2018-07-01', '2018-10-01',
               '2019-01-01', '2019-04-01'],
              dtype='datetime64[ns]', freq='QS-OCT')
In [48]:
# convert a Series with PeriodIndex to a Series with DatetimeIndex
s = pd.Series(range(len(idx)), index = idx)
s.to_timestamp()
Out[48]:
2017-01-01    0
2017-04-01    1
2017-07-01    2
2017-10-01    3
2018-01-01    4
2018-04-01    5
2018-07-01    6
2018-10-01    7
2019-01-01    8
2019-04-01    9
Freq: QS-OCT, dtype: int64

Reference¶

  • Time series / date functionality
  • Pandas Time Series Analysis 5: Period and PeriodIndex
  • Frequency Aliases