admin管理员组

文章数量:1595930

Period(‘2011’, freq = ‘A-DEC’)可以看成多个时间期的时间段中的游标

一、创建PeriodIndex数据类型

1、 pd.PeriodIndex(data=[], freq=‘’)

import pandas as pd
from datetime import datetime

# pd.PeriodIndex()

# 直接直接构建,支持str、datetime.datetime
periods1 = pd.PeriodIndex(data=['2011-1', '2011-2', '2011-3'], freq='M')
periods2 = pd.PeriodIndex(data=[datetime(2015, 6, 1), datetime(2015, 7, 1), datetime(2015, 8, 1)], freq='M')

print("periods1 = {0}, type(periods1) = {1}".format(periods1, type(periods1)))
print("-" * 50)
print("periods1[0] = {0}, type(periods1[0]) = {1}".format(periods1[0], type(periods1[0])))
print("-" * 200)
print("periods2 = {0}, type(periods2) = {1}".format(periods2, type(periods2)))
print("-" * 50)
print("periods2[0] = {0}, type(periods2[0]) = {1}".format(periods2[0], type(periods2[0])))

打印结果:

periods1 = PeriodIndex(['2011-01', '2011-02', '2011-03'], dtype='period[M]', freq='M'), type(periods1) = <class 'pandas.core.indexes.period.PeriodIndex'>
--------------------------------------------------
periods1[0] = 2011-01, type(periods1[0]) = <class 'pandas._libs.tslibs.period.Period'>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
periods2 = PeriodIndex(['2015-06', '2015-07', '2015-08'], dtype='period[M]', freq='M'), type(periods2) = <class 'pandas.core.indexes.period.PeriodIndex'>
--------------------------------------------------
periods2[0] = 2015-06, type(periods2[0]) = <class 'pandas._libs.tslibs.period.Period'>

Process finished with exit code 0

2、 pd.period_range(start=‘’, end=‘’, freq=‘’)

pd.period_range()创建时期范围

import numpy as np
import pandas as pd

# pd.period_range()创建时期范围

# 数据格式为PeriodIndex,单个数值为Period
prng = pd.period_range(start='1/1/2011', end='1/1/2012', freq='M')
print("prng = {0}, type(prng) = {1}".format(prng, type(prng)))
print("-" * 50)
print("prng[0] = {0}, type(prng[0]) = {1}".format(prng[0], type(prng[0])))
print("-" * 100)

打印结果:

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='period[M]', freq='M'), type(prng) = <class 'pandas.core.indexes.period.PeriodIndex'>
--------------------------------------------------
prng[0] = 2011-01, type(prng[0]) = <class 'pandas._libs.tslibs.period.Period'>
----------------------------------------------------------------------------------------------------

二、频率转换

import pandas as pd

# asfreq:频率转换

periods = pd.period_range('2017', '2018', freq='M')
print("periods = ", periods)
print("type(periods) = ", type(periods))
print("-" * 100)

# 通过.asfreq(freq, method=None, how=None)方法转换成别的频率
periods1 = periods.asfreq(freq='D', how='start')
print("periods1 = ", periods1)
print("type(periods1) = ", type(periods1))
print("-" * 100)

打印结果:

periods =  PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06',
             '2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12',
             '2018-01'],
            dtype='period[M]', freq='M')
type(periods) =  <class 'pandas.core.indexes.period.PeriodIndex'>
----------------------------------------------------------------------------------------------------
periods1 =  PeriodIndex(['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01',
             '2017-05-01', '2017-06-01', '2017-07-01', '2017-08-01',
             '2017-09-01', '2017-10-01', '2017-11-01', '2017-12-01',
             '2018-01-01'],
            dtype='period[D]', freq='D')
type(periods1) =  <class 'pandas.core.indexes.period.PeriodIndex'>
----------------------------------------------------------------------------------------------------

Process finished with exit code 0



参考资料:
pandas 周期索引 PeriodIndex
pandas的PeriodIndex的用法

本文标签: 数据类型元素时期类型PeriodIndex