Data Science | Time is silly and unclear

Data Science | Time is silly and unclear

Pandas Period-Period

pd.Period() creation period

Generate a time constructor starting from 2017-01 with monthly frequency:

p = pd.Period('2017', freq ='M')
print(p, type(p))
>>>
2017-01 <class'pandas._period.Period'>

We can move the cycle as a whole by adding and subtracting integers:

p = pd.Period('2017', freq ='M')
print(p, type(p))
print(p + 1)
print(p-2)
>>>
2017-02
2016-11
pd.period_range() Create period range

Create a specified period range:

prng = pd.period_range('1/1/2011', '1/1/2012', freq='M')
print(prng,type(prng))
>>>
PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',
             '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',
             '2012-01'],
            dtype='int64', freq='M') <class'pandas.tseries.period.PeriodIndex'>

Combine the above period series to create a time series:

ts = pd.Series(np.random.rand(len(prng)), index = prng)
print(ts,type(ts))
print(ts.index)
>>>
2011-01 0.342571
2011-02 0.826151
2011-03 0.370505
2011-04 0.137151
2011-05 0.679976
2011-06 0.265928
2011-07 0.416502
2011-08 0.874078
2011-09 0.112801
2011-10 0.112504
2011-11 0.448408
2011-12 0.851046
2012-01 0.370605
Freq: M, dtype: float64 <class'pandas.core.series.Series'>
PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06',
             '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12',
             '2012-01'],
            dtype='int64', freq='M')
pd.period-asfreq: frequency conversion

Through the .asfreq(freq, method=None, how=None) method, the previously generated frequency can be converted into another frequency

p = pd.Period('2017','A-DEC')
print(p)
print(p.asfreq('M', how ='start')) # You can also write how ='s'
print(p.asfreq('D', how ='end')) # You can also write how ='e'
>>>
2017
2017-01
2017-12-31

asfreq can also convert the index of TIMESeries:

prng = pd.period_range('2017','2018',freq ='M')
ts1 = pd.Series(np.random.rand(len(prng)), index = prng)
ts2 = pd.Series(np.random.rand(len(prng)), index = prng.asfreq('D', how ='start'))
print(ts1.head(),len(ts1))
print(ts2.head(),len(ts2))

Conversion between timestamp and period

Use pd.to_period() and pd.to_timestamp() to convert between timestamp and period.

rng = pd.date_range('2017/1/1', periods = 10, freq ='M')
prng = pd.period_range('2017','2018', freq ='M')

ts1 = pd.Series(np.random.rand(len(rng)), index = rng)
print(ts1.head())
print(ts1.to_period().head())
# The last day of each month, converted to each month

ts2 = pd.Series(np.random.rand(len(prng)), index = prng)
print(ts2.head())
print(ts2.to_timestamp().head())
# Monthly, converted to the first day of each month
>>>
2017-01-31 0.125288
2017-02-28 0.497174
2017-03-31 0.573114
2017-04-30 0.665665
2017-05-31 0.263561
Freq: M, dtype: float64
2017-01 0.125288
2017-02 0.497174
2017-03 0.573114
2017-04 0.665665
2017-05 0.263561
Freq: M, dtype: float64
2017-01 0.748661
2017-02 0.095891
2017-03 0.280341
2017-04 0.569813
2017-05 0.067677
Freq: M, dtype: float64
2017-01-01 0.748661
2017-02-01 0.095891
2017-03-01 0.280341
2017-04-01 0.569813
2017-05-01 0.067677
Freq: MS, dtype: float64

Consolidation exercises

  • 1: Please output the following time series, use pd.period_range()
Reference: https://cloud.tencent.com/developer/article/1518481 Data Science | Period and time are silly and unclear-Cloud + Community-Tencent Cloud