Pandas 生成时间序列

发布时间:2024年01月22日

时间序列

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

date_range

  • 可以指定开始时间与周期
  • H:小时
  • D:天
  • M:月
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
rng = pd.date_range('2016/07/01', periods=10, freq='3D')
print(rng)

TIMES的几种书写方式

  • 2016 Jul 1
  • 7/1/2016
  • 1/7/2016?
  • 2016-07-01
  • 2016/07/01?

?pandas.Series

time = pd.Series(np.random.randn(20), index=pd.date_range(dt.datetime(2016,1,1),periods=20))
print(time)
'''
2016-01-01    1.171836
2016-01-02   -1.270964
2016-01-03   -0.635355
2016-01-04   -0.695312
2016-01-05   -0.490697
2016-01-06   -1.047179
2016-01-07    0.895428
2016-01-08   -1.516199
2016-01-09    0.850112
2016-01-10    1.441586
2016-01-11    0.054532
2016-01-12    0.231320
2016-01-13   -1.818684
2016-01-14    0.663896
2016-01-15    2.241499
2016-01-16   -1.172098
2016-01-17   -0.135401
2016-01-18   -1.289502
2016-01-19    0.509668
2016-01-20    0.550763
Freq: D, dtype: float64

'''

truncate过滤?

time.truncate(before='2024-1-10')

1月10之前的都被过滤掉了

时间戳

pd.Timestamp('2016-07-10')
# Timestamp('2016-07-10 00:00:00')

pd.Timestamp('2016-07-10 10')
# Timestamp('2016-07-10 10:00:00')
pd.Timestamp('2016-07-10 10:15')
# Timestamp('2016-07-10 10:15:00')
 

pd.Period('2016-01')
# Period('2016-01', 'M')

?时间偏移

# TIME OFFSETS
pd.Timedelta('1 day')
# Timedelta('1 days 00:00:00')
pd.Period('2016-01-01 10:10') + pd.Timedelta('1 day')
# Period('2016-01-02 10:10', 'T')
pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('1 day')
# Timestamp('2016-01-02 10:10:00')
pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('15 ns')
# Timestamp('2016-01-01 10:10:00.000000015')
 
p1 = pd.period_range('2016-01-01 10:10', freq = '25H', periods = 10)
p2 = pd.period_range('2016-01-01 10:10', freq = '1D1H', periods = 10)

文章来源:https://blog.csdn.net/qq_43010742/article/details/135752924
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。