Python|熊猫 TimedeltaIndex.shift()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas TimedeltaIndex.shift()
函数对给定的 TimedeltaIndex 对象执行专门的移位,从而生成一个 DatetimeIndex 对象。
Syntax : TimedeltaIndex.shift(n, freq=None)
Parameters :
n : Periods to shift by
freq : DateOffset or timedelta-like, optional
Return : shifted : DatetimeIndex
示例 #1:使用TimedeltaIndex.shift()
函数将给定的 TimedeltaIndex 对象移动 2 个句点。
# importing pandas as pd
import pandas as pd
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(start ='11 days 22:14:12.001124',
periods = 5, freq ='T')
# Print the TimedeltaIndex object
print(tidx)
输出 :
现在我们将使用TimedeltaIndex.shift()
函数将给定 TimedeltaIndex 对象的每个元素移动 2 个周期。
# shift by 2 periods
tidx.shift(n = 2)
输出 :
正如我们在输出中看到的, TimedeltaIndex.shift()
函数返回了一个新对象,并将每个元素移动了 2 分钟。
示例 #2:使用TimedeltaIndex.shift()
函数将给定的 TimedeltaIndex 对象移动 2 个句点。
# importing pandas as pd
import pandas as pd
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(start ='03 days 09:22:56',
periods = 5, freq ='H')
# Print the TimedeltaIndex object
print(tidx)
输出 :
现在我们将使用TimedeltaIndex.shift()
函数将给定 TimedeltaIndex 对象的每个元素移动 5 个句点。
# shift by 5 periods
tidx.shift(n = 5)
输出 :
正如我们在输出中看到的, TimedeltaIndex.shift()
函数返回了一个新对象,并将每个元素移动了 5 小时。