pandas Time Series

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12257180.html

Python datetime

python内置模块,主要包含以下几个类:

  • date:使用公历日历存储的日期(年,月,日)
  • time:将时间存储为小时,分钟,秒和微秒
  • datetime:存储日期和时间
  • timedelta:表示两个datetime值之间的差
  • tzinfo:用于存储时区信息的基本类型

字符串 <=> datetime

pandas Time Series

date_range 

Note: start,end,periods,freq这4个参数只能设置3个,否则会报错

ValueError: Of the four parameters: start, end, periods, and freq, exactly three must be specified
  • start: 起始时间
  • end: 结束时间
  • periods: 生成时间序列的数量
  • freq: 频率,默认为'D',即按天,可以使用的频率参考 https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases
  • tz: 时区
  • normalize: 把时间标准化到午夜
  • name: 生成的日期范围的名字
  • closed: {None, 'left', 'right'},是否包含起始或结束时间。closed=left,不包含结束时间,closed=right,不包含起始时间。默认为None,起始和结束都包含。

以时间序列为索引的Series

Resample

resample是指将时间序列从一个频率转换为另一个频率的过程

  • 将高频率的数据聚合到低频率称为 downsample
  • 从低频率的数据转换到高频率称为 upsample

https://pandas.pydata.org/pandas-docs/stable/reference/resampling.html

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.resample.html 

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html

Reference

Python for Data Analysis Second Edition

https://docs.python.org/3/library/datetime.html

https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.date_range.html

https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases

原文地址:https://www.cnblogs.com/agilestyle/p/12257180.html