python pandas时间操作函数

简介: python pandas时间操作函数

代码数据集:

通过上图数据集,使用如下三个时间操作函数进行演示。

  • to_datetime()
  • DateOffset()
  • Datetimeindex()


1 pd.to_datetime()


功能:将str和unicode转化为指定时间戳格式


time=pd.to_datetime('2021-06-10 12:00:00',format='%Y/%m/%d %H:%M:%S')
time:2021/06/10 12:00:00


2 pd.DateOffset()


功能:时间戳的加减

参数:


  • months:设置月
  • days:设置天
  • years:设置年
  • hours:设置小时
  • minutes:设置分钟
  • seconds:设置秒
time=time.to_datetime('2021-06-10')+pd.DateOffset(days=4)
time:2021-06-14 12:00:00


3 pd.Datetimeindex()


将时间list类型转化为时间序列相关的可操作序列


pd.DatetimeIndex(dates)
DatetimeIndex(['1969-09-15 11:38:30', '1965-01-15 11:38:30',
               '1975-07-15 11:38:30', '1964-03-15 11:38:30',
               '1977-03-15 11:38:30', '1972-11-15 11:38:30',
               '1959-06-15 11:38:30', '1970-04-15 11:38:30',
               '1953-01-15 11:38:30', '1979-09-15 11:38:30',
               ...
               '1968-07-15 11:38:30', '1971-03-15 11:38:30',
               '1962-01-15 11:38:30', '1958-07-15 11:38:30',
               '1970-12-15 11:38:30', '1959-11-15 11:38:30',
               '1952-01-15 11:38:30', '1970-02-15 11:38:30',
               '1961-11-15 11:38:30', '1965-12-15 11:38:30'],
              dtype='datetime64[ns]', length=10847, freq=None)
pd.DatetimeIndex(dates).year
Int64Index([1969, 1965, 1975, 1964, 1977, 1972, 1959, 1970, 1953, 1979,
            ...
            1968, 1971, 1962, 1958, 1970, 1959, 1952, 1970, 1961, 1965],
           dtype='int64', length=10847)
相关文章
|
5天前
|
Serverless 数据处理 索引
Pandas中的shift函数:轻松实现数据的前后移动
Pandas中的shift函数:轻松实现数据的前后移动
25 0
|
5天前
|
数据采集 数据可视化 数据挖掘
Pandas函数大合集:数据处理神器一网打尽!
Pandas函数大合集:数据处理神器一网打尽!
16 0
|
4天前
|
数据采集 数据可视化 数据挖掘
Python量化炒股常用的Pandas包
Python量化炒股常用的Pandas包
19 7
|
4天前
|
Python
python函数进阶
python函数进阶
|
3天前
|
安全 Python
Python量化炒股的获取数据函数—get_industry()
Python量化炒股的获取数据函数—get_industry()
11 3
|
4天前
|
Python
Python sorted() 函数和sort()函数对比分析
Python sorted() 函数和sort()函数对比分析
|
3天前
|
Python
Python量化炒股的获取数据函数—get_security_info()
Python量化炒股的获取数据函数—get_security_info()
10 1
|
3天前
|
Python
Python量化炒股的获取数据函数— get_billboard_list()
Python量化炒股的获取数据函数— get_billboard_list()
|
3天前
|
安全 数据库 数据格式
Python量化炒股的获取数据函数—get_fundamentals()
Python量化炒股的获取数据函数—get_fundamentals()
10 0
|
4天前
|
算法 Python
Python编程的函数—内置函数
Python编程的函数—内置函数
下一篇
无影云桌面