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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
