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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通