Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas DatetimeIndex.to_perioddelta()
函數計算索引值與在指定頻率下轉換為periodIndex的索引之差的TimedeltaIndex。它用於矢量化偏移量。
用法: DatetimeIndex.to_perioddelta(freq)
參數:
freq: Pandas 的偏移量字符串或偏移對象之一。將默認推斷
返回:時間指數
範例1:采用DatetimeIndex.to_perioddelta()
函數以指定頻率計算索引值與轉換為periodIndex的索引之間的差值的TimedeltaIndex
# importing pandas as pd
import pandas as pd
# Create the DatetimeIndex
# Here 'S' represents secondly frequency
didx = pd.DatetimeIndex(start ='2018-11-15 09:45:10', freq ='S', periods = 5)
# Print the DatetimeIndex
print(didx)
輸出:
現在我們要計算索引值與轉換為periodIndex的索引之間的差的TimedeltaIndex
# calculate the TimedeltaIndex
# 'T' represents minute based frequency
didx.to_perioddelta('T')
輸出:
正如我們在輸出中看到的,返回的索引值與轉換為periodIndex的索引之間的差。
範例2:采用DatetimeIndex.to_perioddelta()
函數用於計算索引值與在指定頻率下轉換為periodIndex的索引之間的差值的TimedeltaIndex。
# importing pandas as pd
import pandas as pd
# Create the DatetimeIndex
# Here 'T' represents minutely frequency
didx = pd.DatetimeIndex(start ='2015-03-02 01:15:12', freq ='T', periods = 5)
# Print the DatetimeIndex
print(didx)
輸出:
現在我們要計算索引值與轉換為periodIndex的索引之間的差的TimedeltaIndex
# calculate the TimedeltaIndex
# 'H' represents hourly frequency
didx.to_period('H')
輸出:
正如我們在輸出中看到的,返回的索引值與轉換為periodIndex的索引之間的差。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas DatetimeIndex.to_perioddelta()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。