已信任 Jupyter 服务器: 本地 Python 3: Not Started [1] import pandas as pd import numpy as np [2] # 设置时间差,通过字符串 timefiff = pd.Timedelta('2 days 2 hours 15 minutes 30 seconds') timefiff Timedelta('2 days 02:15:30') [3] # 设置时间差,通过数字 timefiff = pd.Timedelta(6,unit='h') timefiff Timedelta('0 days 06:00:00') [5] timediff = pd.Timedelta(days=2) timediff Timedelta('2 days 00:00:00') [6] s = pd.Series(pd.date_range('2020-1-1',periods=3,freq='D')) s 0 2020-01-01 1 2020-01-02 2 2020-01-03 dtype: datetime64[ns] [8] td = pd.Series([pd.Timedelta(days=i) for i in range(3)]) td 0 0 days 1 1 days 2 2 days dtype: timedelta64[ns] [9] # 通过时间差相加 s+td 0 2020-01-01 1 2020-01-03 2 2020-01-05 dtype: datetime64[ns] [10] s-td 0 2020-01-01 1 2020-01-01 2 2020-01-01 dtype: datetime64[ns] [11] s-2*td 0 2020-01-01 1 2019-12-31 2 2019-12-30 dtype: datetime64[ns] [-]