import pandas as pd
import numpy as np
# value, represents the entire period
x = pd.Period(value = '2014') # create a one-year period
q = pd.Period(value = '2014Q1') # create a one-quarter period
m = pd.Period(value = '2014-1', freq = 'D') # create a one-month period
d = pd.Period(value = '2014-1-1', freq = 'H') # create a one-day period
h = pd.Period(value = '2014-1-1, 23:00:00', freq = 'H') # create a one-hour period
x.freq
<YearEnd: month=12>
x.start_time
Timestamp('2014-01-01 00:00:00')
x.end_time
Timestamp('2014-12-31 23:59:59.999999999')
# convert frequency
x.asfreq('D', 'start')
Period('2014-01-01', 'D')
m+1 # unit depends on frequency
Period('2014-01-02', 'D')
h + pd.offsets.Hour(2)
Period('2014-01-02 01:00', 'H')
import datetime
h + datetime.timedelta(minutes=120)
Period('2014-01-02 01:00', 'H')
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
PeriodIndex(['2012-01', '2012-02', '2012-03'], dtype='period[M]')
idx = pd.period_range('2017', '2018', freq='Q') # PeriodIndex
idx
PeriodIndex(['2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1'], dtype='period[Q-DEC]')
idx = pd.period_range('2017', periods = 10, freq='Q')
idx
PeriodIndex(['2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1', '2018Q2', '2018Q3', '2018Q4', '2019Q1', '2019Q2'], dtype='period[Q-DEC]')
# change period data type and resample
idx.astype("period[D]")
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]')
idx.start_time
DatetimeIndex(['2017-01-01', '2017-04-01', '2017-07-01', '2017-10-01', '2018-01-01'], dtype='datetime64[ns]', freq='QS-OCT')
idx.shape
(5,)
idx[0] # Period
Period('2017Q1', 'Q-DEC')
idx[0].start_time
Timestamp('2017-01-01 00:00:00')
s = pd.Series(range(len(idx)), index = idx)
s
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
s['2017']
2017Q1 0 2017Q2 1 2017Q3 2 2017Q4 3 Freq: Q-DEC, dtype: int64
s['2017':'2018']
2017Q1 0 2017Q2 1 2017Q3 2 2017Q4 3 2018Q1 4 2018Q2 5 2018Q3 6 2018Q4 7 Freq: Q-DEC, dtype: int64
st = s.to_timestamp() # convert Period Index to Datetime Index
st, type(st.index), st.index[0]
(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'))
st.to_period() # convert DateTime Index to Period Index
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
# convert PeriodIndex to DatetimeIndex
idx.to_timestamp()
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')
# convert a Series with PeriodIndex to a Series with DatetimeIndex
s = pd.Series(range(len(idx)), index = idx)
s.to_timestamp()
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