温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

pandas时间偏移的实现方法

发布时间:2021-08-09 02:17:31 来源:亿速云 阅读:393 作者:chen 栏目:开发技术

本篇内容主要讲解“pandas时间偏移的实现方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“pandas时间偏移的实现方法”吧!

目录
  • 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就可以。在此不一一列举。

到此,相信大家对“pandas时间偏移的实现方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI