5. Period and its arithmetic operation
The period (period) represents a time interval, such as days, months, seasons, years, etc. The period class represents this data type, and its constructors need to use a string or integer, as well as a frequency.
#-*-coding:utf-8-*-ImportNumPy as NPImportPandas as PDImportMatplotlib.pyplot as PltImportdatetime as DT fromPandasImportSeries,dataframe fromDatetimeImportdatetime fromDateutil.parserImportParseImport Time fromPandas.tseries.offsetsImportHour,minute,day,monthendImportPytz#the following ' A-dec ' is the last calendar day of the end of the year Decemberp = PD. Period (' .', freq ='A-dec')#period can be directly added and reducedPrintP + 5#period of the same frequency can be added and reduced, and different frequencies cannot be added or reduced.RNG = PD. Period (' -', freq ='A-dec') -PPrintrngrng= Pd.period_range ('1/1/2000','6/30/2000', freq ='M')#The type is <class ' Pandas.tseries.period.PeriodIndex ', which is formally an array of arrays#Note that the following form is not the form of the book, but the float type, but when indexed, the date formPrintrngPrinttype (RNG)PrintSeries (NP.RANDOM.RANDN (6), index = RNG),'\ n'#the constructors of the Periodindex class also allow direct use of a set of stringsvalues = ['2001q3','2002Q2','2003q1']index= PD. Periodindex (Values,freq ='Q-dec')#the following indexPrintIndex
>>>
2021
-1
Array ([361, 362, 363, 364, 365], Dtype=int64)
<class ' Pandas.tseries.period.PeriodIndex ' >
2000-01-0.504031
2000-02 1.345024
2000-03 0.074367
2000-04-1.152187
2000-05-0.460272
2000-06 0.486135
Array ([126, 129, PPI], Dtype=int64)
[Finished in 1.4s]
"Data analysis using Python" reading notes--tenth Chapter time series (II.)