python pandas时序处理相关功能详解
2019-07-03 09:00
190 查看
创建时间序列
函数pd.date_range()
根据指定的范围,生成时间序列DatetimeIndex,每隔元素的类型为Timestamp。该函数应用较多。
ts = pd.date_range('2017-09-01', periods=10, freq='d', normalize=False) ts
输出为:
DatetimeIndex(['2017-09-01', '2017-09-02', '2017-09-03', '2017-09-04', '2017-09-05', '2017-09-06', '2017-09-07', '2017-09-08', '2017-09-09', '2017-09-10'], dtype='datetime64[ns]', freq='D'
主要的入参解析:
- start: 开始时刻,可以是字符串或者datetime类型的值。默认None。
- end: 结束时刻,可以是字符串或者datetime类型的值,如果指定了长度,即periods,则可不设置。默认None。
- periods: 时序的长度,整型类型。如果有end,可不设置。默认None。
- freq: 时序生成的频率,即每隔多少时刻生成一个时序点。字符串类型或者DateOffset类型。默认'D',即天粒度,见上述代码输出。
- tz: 时区,字符串类型。默认None。
- normalize: bool类型,没用过,不知道干啥的。
- name: 设置时序的名称,字符串类型,默认None。
- closed: 是否包含两边的值。默认None,即两边都保留。
其中,freq的取值可以为如下的符号表示间隔,可以结合符号和数字,如'3d',表示每隔三天记录一个时间点。大小写都可以。
B business day frequency C custom business day frequency (experimental) D calendar day frequency W weekly frequency M month end frequency SM semi-month end frequency (15th and end of month) BM business month end frequency CBM custom business month end frequency MS month start frequency SMS semi-month start frequency (1st and 15th) BMS business month start frequency CBMS custom business month start frequency Q quarter end frequency BQ business quarter endfrequency QS quarter start frequency BQS business quarter start frequency A year end frequency BA business year end frequency AS year start frequency BAS business year start frequency BH business hour frequency H hourly frequency T, min minutely frequency S secondly frequency L, ms milliseconds U, us microseconds N nanoseconds
字符串转换为时间戳
pd.to_datetime() 函数可以将表示时间的字符串转换位TimeStamp。
pd.to_datetime('2017-09-01')
输出为:
Timestamp('2017-09-01 00:00:00')
常用的参数:
format: 用来设置字符串的格式,默认如上所示。
时间戳的加减
有时候需要将时间进行增减,可以使用类型:DateOffset。
pd.to_datetime('2017-09-01') + pd.DateOffset(days=10)
输出为:
Timestamp('2017-09-11 00:00:00')
DateOffset常用的参数:
- months,设置月。
- days,设置天。
- years,设置年。
- hours,设置小时。
- minutes,设置分钟。
- seconds,设置秒。
以上可以同时设置,组合使用。
pd.to_datetime('2017-09-01') + pd.DateOffset(seconds=10, days = 10)
输出为:
Timestamp('2017-09-11 00:00:10')
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- 详解使用Python处理文件目录的相关方法
- 详解Python的Django框架中的Cookie相关处理
- 详解Python异常处理中的Finally else的功能
- python时间日期函数与利用pandas进行时间序列处理详解
- Python利用pandas处理Excel数据的应用详解
- 详解使用Python处理文件目录的相关方法
- [原创]python之简单计算器(超详解,只有基本功能+-*/,还有括号处理)
- 详解Python的Django框架中的Cookie相关处理
- python Pandas库基础分析之时间序列的处理详解
- Python3.5 Pandas模块缺失值处理和层次索引实例详解
- 详解在Python中处理异常的教程
- Python实现多线程抓取网页功能实例详解
- Python 递归处理文件和文件夹代码详解
- 人工智能:python 实现 第十一章,使用Pandas处理时间序列数据
- python.pandas.pct_change()处理nan的默认值问题
- python时间处理详解
- 【Python】“pandas”库“to_sql”报错“Invalid MySQL identifier”处理记录
- python 视频处理,提取视频相关帧,读取Excel
- 浅析Python中MySQLdb的事务处理功能
- python——常用功能之文本处理