pandas 時(shí)間偏移的實(shí)現(xiàn)
時(shí)間偏移就是在指定時(shí)間往前推或者往后推一段時(shí)間,即加減一段時(shí)間之后的時(shí)間
python中主要有2種方式:一種是借助timedelta,另一種是pandas中的日期偏移量date offset
1 timedelta
1.1 時(shí)間偏移單位為周
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 時(shí)間偏移單位為天
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 時(shí)間偏移單位為小時(shí)
1.3.1 往后推1小時(shí)
date = datetime(2007, 5, 12, 18, 53, 32) print(date + timedelta(hours=1))
result:
2007-05-12 19:53:32
1.3.2 往前推1小時(shí)
date = datetime(2007, 5, 12, 18, 53, 32) print(date - timedelta(hours=1))
result:
2007-05-12 17:53:32
1.4 時(shí)間偏移單位為分鐘
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 時(shí)間偏移單位為秒
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 時(shí)間偏移單位為毫秒
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 時(shí)間偏移單位為微秒
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 時(shí)間偏移單位為天
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
其他時(shí)間單位與timedelta差不多,單位為周、小時(shí)、分鐘、秒時(shí)只要將Day相應(yīng)的換為Week, Hour, Minute, Second就可以。在此不一一列舉。
到此這篇關(guān)于pandas 時(shí)間偏移的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)pandas 時(shí)間偏移內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信