Pandas-时间序列(一)-数据类型:TimeSeries(时间序列)【以 DatatimeIndex/PeriodIndex 为 Index 的 Series】【“时间戳”与“时期”之间的转换】

Timestamp、Period作为index时区别不大

  • Timestamp表示一个时间戳,是一个时间截面;
  • Period是一个时期,是一个时间段!!

一、以DatetimeIndex(元素为:Timestamp)为index

以DatetimeIndex为index的Series,为TimeSries(时间序列)

import numpy as np
import pandas as pd

# pd.DatetimeIndex()
# 直接生成时间戳索引,支持str、datetime.datetime
# 单个时间戳为Timestamp,多个时间戳为DatetimeIndex
rng = pd.DatetimeIndex(['12/1/2017', '12/2/2017', '12/3/2017'])
print("rng = {0}, type(rng) = {1}".format(rng, type(rng)))
print("rng[0] = {0}, type(rng[0]) = {1}".format(rng[0], type(rng[0])))
print('-' * 100)

# TimeSries
# 以DatetimeIndex为index的Series,为TimeSries(时间序列)
st = pd.Series(np.random.rand(len(rng)), index=rng)
print("st = \n{0} \ntype(st) = {1}".format(st, type(st)))
print('-' * 50)
print("st.index = ", st.index)

打印结果:

rng = DatetimeIndex(['2017-12-01', '2017-12-02', '2017-12-03'], dtype='datetime64[ns]', freq=None), type(rng) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
rng[0] = 2017-12-01 00:00:00, type(rng[0]) = <class 'pandas._libs.tslibs.timestamps.Timestamp'>
----------------------------------------------------------------------------------------------------
st = 
2017-12-01    0.310079
2017-12-02    0.571608
2017-12-03    0.557365
dtype: float64 
type(st) = <class 'pandas.core.series.Series'>
--------------------------------------------------
st.index =  DatetimeIndex(['2017-12-01', '2017-12-02', '2017-12-03'], dtype='datetime64[ns]', freq=None)

Process finished with exit code 0

二、以PeriodIndex(元素为:Period)为index

import numpy as np
import pandas as pd

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

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


# 时间序列
# Period('2011', freq = 'A-DEC')可以看成多个时间期的时间段中的游标
# Timestamp表示一个时间戳,是一个时间截面;Period是一个时期,是一个时间段!!但两者作为index时区别不大
ts = pd.Series(np.random.rand(len(periods)), index=periods)
print("ts = \n{0} \ntype(ts) = {1}".format(ts, type(ts)))
print("-" * 50)
print("ts.index = \n", ts.index)

打印结果:

periods = 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(periods) = <class 'pandas.core.indexes.period.PeriodIndex'>
--------------------------------------------------
periods[0] = 2011-01, type(periods[0]) = <class 'pandas._libs.tslibs.period.Period'>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ts = 
2011-01    0.454952
2011-02    0.854056
2011-03    0.255570
2011-04    0.066163
2011-05    0.313975
2011-06    0.682848
2011-07    0.494482
2011-08    0.737051
2011-09    0.883112
2011-10    0.137797
2011-11    0.255970
2011-12    0.802554
2012-01    0.091610
Freq: M, dtype: float64 
type(ts) = <class 'pandas.core.series.Series'>
--------------------------------------------------
ts.index = 
 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')

Process finished with exit code 0

三、时间戳与时期之间的转换:pd.to_period()、pd.to_timestamp()

import numpy as np
import pandas as pd

# 时间戳与时期之间的转换:pd.to_period()、pd.to_timestamp()

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

print("rng = {0} \ntype(rng) = {1}".format(rng, type(rng)))
print("-" * 100)
print("prng = {0} \ntype(prng) = {1}".format(prng, type(prng)))
print("-" * 200)

# 每月最后一日,转化为每月
ts1 = pd.Series(np.random.rand(len(rng)), index=rng)
print("type(ts1.index)) = {}".format(type(ts1.index)))
print("ts1.head() = \n", ts1.head())
print("-" * 100)
ts11 = ts1.to_period()
print("ts11 = ts1.to_period()---->type(ts11.index)) = {}".format(type(ts11.index)))
print("ts11 = ts1.to_period()---->ts11.head() = \n", ts11.head())
print("-" * 200)

# 每月,转化为每月第一天
ts2 = pd.Series(np.random.rand(len(prng)), index=prng)
print("type(ts2.index)) = {}".format(type(ts2.index)))
print("ts2.head() = \n", ts2.head())
print("-" * 100)
ts22 = ts2.to_timestamp()
print("ts22 = ts2.to_timestamp()---->type(ts22.index)) = {}".format(type(ts22.index)))
print("ts22 = ts2.to_timestamp()---->ts22.head() = \n", ts22.head())
print("-" * 200)

打印结果:

rng = DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30',
               '2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',
               '2017-09-30', '2017-10-31'],
              dtype='datetime64[ns]', freq='M') 
type(rng) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
----------------------------------------------------------------------------------------------------
prng = 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(prng) = <class 'pandas.core.indexes.period.PeriodIndex'>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
type(ts1.index)) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
ts1.head() = 
 2017-01-31    0.307686
2017-02-28    0.752647
2017-03-31    0.378614
2017-04-30    0.519277
2017-05-31    0.382742
Freq: M, dtype: float64
----------------------------------------------------------------------------------------------------
ts11 = ts1.to_period()---->type(ts11.index)) = <class 'pandas.core.indexes.period.PeriodIndex'>
ts11 = ts1.to_period()---->ts11.head() = 
 2017-01    0.307686
2017-02    0.752647
2017-03    0.378614
2017-04    0.519277
2017-05    0.382742
Freq: M, dtype: float64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
type(ts2.index)) = <class 'pandas.core.indexes.period.PeriodIndex'>
ts2.head() = 
 2017-01    0.804532
2017-02    0.820868
2017-03    0.065087
2017-04    0.016507
2017-05    0.040606
Freq: M, dtype: float64
----------------------------------------------------------------------------------------------------
ts22 = ts2.to_timestamp()---->type(ts22.index)) = <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
ts22 = ts2.to_timestamp()---->ts22.head() = 
 2017-01-01    0.804532
2017-02-01    0.820868
2017-03-01    0.065087
2017-04-01    0.016507
2017-05-01    0.040606
Freq: MS, dtype: float64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Process finished with exit code 0
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值