机器学习实战3.1之基于pandas的时间序列

版权声明:此文章有作者原创,涉及相关版本问题可以联系作者,[email protected] https://blog.csdn.net/weixin_42600072/article/details/88898481
import numpy as np
import pandas as pd

时间序列

  • 时间戳(timestamp)
  • 固定周期(period)
  • 时间间隔(interval)

date_range

  • 可以指定开始时间与周期,也可以与常数结合
  • H:小时
  • D:天(默认参数)
  • M:月
# TIMES #2019 March 1 3/1/2019 1/3/2019 2019-03-01 2019/03/01
rng = pd.date_range('2019-3-29', periods=10, freq='2D')
rng
DatetimeIndex(['2019-03-29', '2019-03-31', '2019-04-02', '2019-04-04',
               '2019-04-06', '2019-04-08', '2019-04-10', '2019-04-12',
               '2019-04-14', '2019-04-16'],
              dtype='datetime64[ns]', freq='2D')

生成时间戳序列

import datetime as dt
time = pd.Series(
    np.random.randn(20),
    index=pd.date_range(dt.datetime(2019, 3, 29), periods=20))
print(time)
2019-03-29   -0.484120
2019-03-30    0.017819
2019-03-31   -0.201267
2019-04-01    0.752419
2019-04-02   -0.326697
2019-04-03    0.154714
2019-04-04   -1.991689
2019-04-05    1.946314
2019-04-06   -0.191235
2019-04-07    0.431885
2019-04-08    0.333982
2019-04-09    0.255393
2019-04-10   -0.955682
2019-04-11   -1.287640
2019-04-12   -1.757973
2019-04-13    0.244157
2019-04-14   -1.897992
2019-04-15   -0.380028
2019-04-16    0.531759
2019-04-17    0.469582
Freq: D, dtype: float64
  • truncate过滤
time.truncate(before='2019-04-05')
2019-04-05    1.946314
2019-04-06   -0.191235
2019-04-07    0.431885
2019-04-08    0.333982
2019-04-09    0.255393
2019-04-10   -0.955682
2019-04-11   -1.287640
2019-04-12   -1.757973
2019-04-13    0.244157
2019-04-14   -1.897992
2019-04-15   -0.380028
2019-04-16    0.531759
2019-04-17    0.469582
Freq: D, dtype: float64
time.truncate(after='2019-04-05')
2019-03-29   -0.484120
2019-03-30    0.017819
2019-03-31   -0.201267
2019-04-01    0.752419
2019-04-02   -0.326697
2019-04-03    0.154714
2019-04-04   -1.991689
2019-04-05    1.946314
Freq: D, dtype: float64
print(time['2019-04-05'])
1.9463144930513656
print(time['2019-04-05' : '2019-04-09'])
2019-04-05    1.946314
2019-04-06   -0.191235
2019-04-07    0.431885
2019-04-08    0.333982
2019-04-09    0.255393
Freq: D, dtype: float64
date = pd.date_range('2019-3-29', '2019-8-15', freq='M')
date
DatetimeIndex(['2019-03-31', '2019-04-30', '2019-05-31', '2019-06-30',
               '2019-07-31'],
              dtype='datetime64[ns]', freq='M')

时间戳

pd.Timestamp('2019.3.29')
Timestamp('2019-03-29 00:00:00')
# 可以指定更多细节
pd.Timestamp('2019.03.29 10:15')
Timestamp('2019-03-29 10:15:00')

时间区间

pd.Period('2019.03.29')
Period('2019-03-29', 'D')
pd.Period('2019-03')
Period('2019-03', 'M')

时间偏移

pd.Timedelta('1 day')
Timedelta('1 days 00:00:00')
pd.Period('2019.3.29 10:10') + pd.Timedelta('1 day')
Period('2019-03-30 10:10', 'T')
pd.Timestamp('2019.03.29 10:15') + pd.Timedelta('1 day')
Timestamp('2019-03-30 10:15:00')
p = pd.period_range('2016-01-01 10:10', freq = '1D1H', periods = 10)
p
PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00',
             '2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00',
             '2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00',
             '2016-01-10 19:00'],
            dtype='period[25H]', freq='25H')

指定索引

rng = pd.date_range('2019-3-29', periods=10, freq='D')
rng
pd.Series(range(len(rng)), index=rng)
2019-03-29    0
2019-03-30    1
2019-03-31    2
2019-04-01    3
2019-04-02    4
2019-04-03    5
2019-04-04    6
2019-04-05    7
2019-04-06    8
2019-04-07    9
Freq: D, dtype: int64
periods = [pd.Period('2016-01'), pd.Period('2016-02'), pd.Period('2016-03')]
ts = pd.Series(np.random.randn(len(periods)), index = periods)
ts
2016-01   -0.508165
2016-02   -0.303909
2016-03   -0.885760
Freq: M, dtype: float64
print(type(ts.index))
<class 'pandas.core.indexes.period.PeriodIndex'>
# 时间戳和时间周期可以转换
ts = pd.Series(range(10), pd.date_range('29-3-2019 8:00', periods = 10, freq = 'H'))
ts
2019-03-29 08:00:00    0
2019-03-29 09:00:00    1
2019-03-29 10:00:00    2
2019-03-29 11:00:00    3
2019-03-29 12:00:00    4
2019-03-29 13:00:00    5
2019-03-29 14:00:00    6
2019-03-29 15:00:00    7
2019-03-29 16:00:00    8
2019-03-29 17:00:00    9
Freq: H, dtype: int64
ts_period = ts.to_period()
ts_period
2019-03-29 08:00    0
2019-03-29 09:00    1
2019-03-29 10:00    2
2019-03-29 11:00    3
2019-03-29 12:00    4
2019-03-29 13:00    5
2019-03-29 14:00    6
2019-03-29 15:00    7
2019-03-29 16:00    8
2019-03-29 17:00    9
Freq: H, dtype: int64

猜你喜欢

转载自blog.csdn.net/weixin_42600072/article/details/88898481