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


猜你喜欢
完美解决python遍历删除字典里值为空的元素报错问题
virtualenv 指定 python 解释器的版本方法
$.browser.msie 为空或不是对象问题的多种解决方法
将Python字符串生成PDF的实例代码详解

Python NumPy库安装使用笔记
学习python分支结构
详解pandas.DataFrame中删除包涵特定字符串所在的行

MongoDB为用户设置访问权限
20分钟MySQL基础入门

BootStrap modal实现拖拽功能
深入了解JavaScript代码覆盖

Python图像处理之图像的缩放、旋转与翻转实现方法示例

SQL学习笔记三 select语句的各种形式小结
mysql -参数thread_cache_size优化方法 小结

10个提高网站可用性的实用技巧[译]

对numpy和pandas中数组的合并和拆分详解
python实现人机对战的五子棋游戏

Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例
总结Python图形用户界面和游戏开发知识点
Python 使用Numpy对矩阵进行转置的方法
