目錄
- 1 timedelta
- 1.1 時間偏移單位為周
- 1.2 時間偏移單位為天
- 1.3 時間偏移單位為小時
- 1.4 時間偏移單位為分鐘
- 1.5 時間偏移單位為秒
- 1.6 時間偏移單位為毫秒
- 1.7 時間偏移單位為微秒
- 2 date offset
時間偏移就是在指定時間往前推或者往后推一段時間,即加減一段時間之后的時間
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相應(yīng)的換為Week, Hour, Minute, Second就可以。在此不一一列舉。
到此這篇關(guān)于pandas 時間偏移的實現(xiàn)的文章就介紹到這了,更多相關(guān)pandas 時間偏移內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 對pandas中時間窗函數(shù)rolling的使用詳解
- python+pandas+時間、日期以及時間序列處理方法
- pandas的to_datetime時間轉(zhuǎn)換使用及學(xué)習(xí)心得
- pandas 時間格式轉(zhuǎn)換的實現(xiàn)
- pandas的object對象轉(zhuǎn)時間對象的方法
- python pandas 時間日期的處理實現(xiàn)
- python pandas生成時間列表
- pandas基于時間序列的固定時間間隔求均值的方法
- python Pandas庫基礎(chǔ)分析之時間序列的處理詳解
- Python Pandas數(shù)據(jù)中對時間的操作