Pandas-时期(二)-数据类型:PeriodIndex(其中每个元素的类型为:Period)【创建:①pd.PeriodIndex([], freq=‘‘);②pd.period_range()】

本文介绍了如何使用Pandas创建PeriodIndex,包括通过数据列表和datetime对象,以及利用period_range函数。同时,展示了如何使用.asfreq方法进行频率转换,将月度数据转换为每日数据。这些内容对于数据处理和时间序列分析至关重要。
摘要由CSDN通过智能技术生成

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的用法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值