pandas 时间偏移的实现

作者:不思量自难忘 时间:2021-09-24 03:54:01 

目录
  • 1 timedelta

    • 1.1 时间偏移单位为周

    • 1.2 时间偏移单位为天

    • 1.3 时间偏移单位为小时

    • 1.4 时间偏移单位为分钟

    • 1.5 时间偏移单位为秒

    • 1.6 时间偏移单位为毫秒

    • 1.7 时间偏移单位为微秒

  • 2 date offset

    • 2.1 时间偏移单位为天

时间偏移就是在指定时间往前推或者往后推一段时间,即加减一段时间之后的时间

python中主要有2种方式:一种是借助timedelta,另一种是pandas中的日期偏移量date offset

1 timedelta

1.1 时间偏移单位为周

1.1.1 往后推1周


date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(weeks=1))

result:

2007-05-19 18:53:32

1.1.2 往前推1周


date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(weeks=1))

result:

2007-05-05 18:53:32

1.2 时间偏移单位为天

1.2.1 往后推1天


from datetime import timedelta, datetime

date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(days=1))

result:

2007-05-13 18:53:32

1.2.2 往前推1天


date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(days=1))

result:

2007-05-11 18:53:32

1.3 时间偏移单位为小时

1.3.1 往后推1小时


date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(hours=1))

result:

2007-05-12 19:53:32

1.3.2 往前推1小时


date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(hours=1))

result:

2007-05-12 17:53:32

1.4 时间偏移单位为分钟

1.4.1 往后推1分钟


date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(minutes=1))

result:

2007-05-12 18:54:32

1.4.2 往前推1分钟


date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(minutes=1))

result:

2007-05-12 18:52:32

1.5 时间偏移单位为秒

1.5.1 往后推1秒


date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(seconds=1))

result:

2007-05-12 18:53:33

1.5.2 往前推1秒


date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(seconds=1))

result:

2007-05-12 18:53:31

1.6 时间偏移单位为毫秒

1.6.1 往后推1毫秒


date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date + timedelta(milliseconds=1))

result:

2007-05-12 18:53:32.001987

1.6.2 往前推1毫秒


date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date - timedelta(milliseconds=1))

result:

2007-05-12 18:53:31.999987

1.7 时间偏移单位为微秒

1.7.1 往后推1微秒


date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date + timedelta(microseconds=1))

result:

2007-05-12 18:53:32.000988

1.7.2 往前推1微秒


date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date - timedelta(microseconds=1))

result:

2007-05-12 18:53:32.000986

2 date offset


from datetime import datetime
from pandas.tseries.offsets import Day
date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date + Day(1))

result:

2007-05-13 18:53:32.000987

2.1 时间偏移单位为天

2.1.1 往后推1天


date = datetime(2007, 5, 12, 18, 53, 32)
print(date + Day(1))

result:

2007-05-13 18:53:32

2.1.2 往前推1天


date = datetime(2007, 5, 12, 18, 53, 32,)
print(date - Day(1))

result:

2007-05-11 18:53:32

其他时间单位与timedelta差不多,单位为周、小时、分钟、秒时只要将Day相应的换为Week, Hour, Minute, Second就可以。在此不一一列举。

来源:https://juejin.cn/post/6993247212880789512

标签:pandas,时间偏移
0
投稿

猜你喜欢

  • python并发编程 Process对象的其他属性方法join方法详解

    2022-03-07 04:29:54
  • python中opencv 直方图处理

    2021-12-24 09:45:17
  • 解决MSSQL下“不能在手动或分布事务方式下创建新的连接”的问题

    2008-07-15 12:48:00
  • RC4经典加密算法asp/VBs版本代码

    2008-02-17 17:32:00
  • python+pytest接口自动化之token关联登录的实现

    2023-01-21 13:27:37
  • 用Python定时发送天气邮件

    2022-09-22 15:11:31
  • 基于Python pyecharts实现多种图例代码解析

    2021-11-10 05:20:17
  • 浅谈python下含中文字符串正则表达式的编码问题

    2022-04-08 01:18:35
  • XML简易教程之一

    2008-09-05 17:19:00
  • Pytorch mask-rcnn 实现细节分享

    2021-10-20 01:31:38
  • Python中的Classes和Metaclasses详解

    2022-07-08 09:28:47
  • 基于Python实现文件分类器的示例代码

    2023-06-02 12:49:10
  • 在Pytorch中使用Mask R-CNN进行实例分割操作

    2023-05-18 21:41:22
  • Python实现读取txt文件并转换为excel的方法示例

    2023-07-25 15:20:16
  • 用python3读取python2的pickle数据方式

    2023-06-05 09:45:48
  • Python子类继承父类构造函数详解

    2023-02-27 09:13:03
  • 批标准化层 tf.keras.layers.Batchnormalization()解析

    2023-06-18 23:35:17
  • mysql导入导出命令

    2011-07-04 11:28:50
  • 基于python3 OpenCV3实现静态图片人脸识别

    2022-10-04 20:59:30
  • MySQL数据库的临时文件究竟储存在哪里

    2009-02-13 13:44:00
  • asp之家 网络编程 m.aspxhome.com