Pandas DataFrame.tshift(~) 方法會移動源 DataFrame 的日期索引。
警告
tshift(~) 現已棄用 - 請使用 shift(~) 代替。
參數
1.periods | int | optional
要移動的量。如果axis=0(默認值),則正整數表示行向下移動,而負整數表示行向上移動。相同的邏輯適用於axis=1(列移位)。
默認情況下,periods=1 。
2. freq | DateOffset 或 timedelta 或 string | optional
要移動的時間單位。默認情況下,freq=None 。
3. axis | int 或 string | optional
是否移動行索引或列索引:
|
軸 |
說明 |
|---|---|
|
|
移動行索引。 |
|
|
移動列索引。 |
默認情況下,axis=0 。
返回值
具有移位索引的新DataFrame。
例子
考慮以下 DataFrame :
date_index = pd.date_range("2020/12/25", periods=4)
df = pd.DataFrame({"A":[2,3,4,5],"B":[6,7,8,9]}, index=date_index)
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9
這裏, df 的索引是 DatetimeIndex 類型。
周期參數
要將行索引移動 1 天:
df.tshift()
A B
2020-12-26 2 6
2020-12-27 3 7
2020-12-28 4 8
2020-12-29 5 9
要將行索引移動 2 天:
df.tshift(periods=2)
A B
2020-12-27 2 6
2020-12-28 3 7
2020-12-29 4 8
2020-12-30 5 9
將 back-shift 行索引增加 1 天,設置一個負周期,如下所示:
df.tshift(periods=-1)
A B
2020-12-24 2 6
2020-12-25 3 7
2020-12-26 4 8
2020-12-27 5 9
頻率參數
考慮與上麵相同的DataFrame:
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9
默認情況下,freq 是從日期索引推斷的。由於我們的 df 有一個 freq 一天,即每個日期索引在一天內變化,因此 tshift 將使用 freq="D" :
df.tshift(periods=1) # freq="D"
A B
2020-12-26 2 6
2020-12-27 3 7
2020-12-28 4 8
2020-12-29 5 9
這裏,每個日期索引移動 1 天單位。以 3 天為單位移動,例如:
df.tshift(periods=1, freq="3D")
A B
2020-12-28 2 6
2020-12-29 3 7
2020-12-30 4 8
2020-12-31 5 9
請注意,這本質上與指定 periods=3 具有相同的效果:
df.tshift(periods=3)
A B
2020-12-28 2 6
2020-12-29 3 7
2020-12-30 4 8
2020-12-31 5 9
警告
考慮以下 DataFrame :
date_index = pd.date_range("2020/12/30", periods=4)
df = pd.DataFrame({"A":[2,3,4,5],"B":[6,7,8,9]}, index=date_index)
df
A B
2020-12-30 2 6
2020-12-31 3 7
2021-01-01 4 8
2021-01-02 5 9
設置 freq="M" 會產生:
df.tshift(periods=1, freq="M")
A B
2020-12-31 2 6
2021-01-31 3 7
2021-01-31 4 8
2021-01-31 5 9
結果有點不直觀,因為我們期望將 one-month 偏移量應用於每個日期索引。
請注意以下事項:
-
每個日期索引都轉移到月底。
-
由於我們指定了
freq="M",因此日期索引的日單位變得無關緊要。 -
例外情況是當日單位位於月末時(例如
12-31),在這種情況下,月份將發生偏移(例如01-31)。
相關用法
- Python Pandas DataFrame tz_convert方法用法及代碼示例
- Python Pandas DataFrame tail方法用法及代碼示例
- Python Pandas DataFrame transform方法用法及代碼示例
- Python Pandas DataFrame truncate方法用法及代碼示例
- Python Pandas DataFrame to_csv方法用法及代碼示例
- Python Pandas DataFrame tz_localize方法用法及代碼示例
- Python Pandas DataFrame truediv方法用法及代碼示例
- Python Pandas DataFrame transpose方法用法及代碼示例
- Python PySpark DataFrame toDF方法用法及代碼示例
- Python PySpark DataFrame toJSON方法用法及代碼示例
- Python Pandas DataFrame to_period方法用法及代碼示例
- Python Pandas DataFrame take方法用法及代碼示例
- Python Pandas DataFrame to_json方法用法及代碼示例
- Python PySpark DataFrame tail方法用法及代碼示例
- Python PySpark DataFrame toPandas方法用法及代碼示例
- Python Pandas DataFrame to_timestamp方法用法及代碼示例
- Python Pandas DataFrame to_numpy方法用法及代碼示例
- Python PySpark DataFrame transform方法用法及代碼示例
- Python Pandas DataFrame to_dict方法用法及代碼示例
- Python PySpark DataFrame take方法用法及代碼示例
- Python Pandas DataFrame empty屬性用法及代碼示例
- Python Pandas DataFrame pop方法用法及代碼示例
- Python Pandas DataFrame nsmallest方法用法及代碼示例
- Python Pandas DataFrame sample方法用法及代碼示例
- Python Pandas DataFrame items方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas DataFrame | tshift method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
