Pandas PeriodIndex(~)構造函數創建一個新的PeriodIndex對象,代表一個特定的時間跨度或持續時間.PeriodIndex最常用作 DataFrame 的索引。
警告
不要直接調用這個構造函數,而是選擇使用 period_range(~) ,這樣更靈活,使用也更廣泛。
參數
1.data | array-like | optional
類似數組,包含構造 PeriodIndex 的日期。
2. copy | boolean | optional
- 
如果 True,則將返回data的新副本 - 修改此複製的數據不會改變原始data,反之亦然。
- 
如果 False,則返回的 PeriodIndex 將使用與data相同的內存塊。這意味著修改返回值將改變原始的data,反之亦然。
默認情況下,copy=False 。
3. freq | string 或 Period | optional
PeriodIndex 的頻率。您可以將 freq 視為時間跨度的間隔大小。默認情況下,頻率是從 data 推斷的。
4. year | int 或 array 或 Series | optional
默認情況下,year=None 。
5. month | int 或 array 或 Series | optional
默認情況下,month=None 。
6. quarter | int 或 array 或 Series | optional
默認情況下,quarter=None 。
7. day | int 或 array 或 Series | optional
默認情況下,day=None 。
8. hour | int 或 array 或 Series | optional
默認情況下,hour=None 。
9. minute | int 或 array 或 Series | optional
默認情況下,minute=None 。
10.second | int 或 array 或 Series | optional
默認情況下,second=None 。
11.tz | object | optional
從 datetime64 轉換為 PeriodIndex 時使用的時區。默認情況下,tz=None 。
返回值
PeriodIndex 對象。
例子
基本用法
要使用日期字符串列表創建PeriodIndex:
index_period = pd.PeriodIndex(["2020-12-25", "2020-12-26"], freq="D")
index_period
PeriodIndex(['2020-12-25', '2020-12-26'], dtype='period[D]', freq='D')這裏,freq="D"表示每個周期的間隔大小是一天。
然後我們可以使用PeriodIndex作為DataFrame的索引,如下所示:
pd.DataFrame({"A":["a","b"]}, index=index_period)
            A
2020-12-25  a
2020-12-26  b指定頻率
要指定 PeriodIndex 的間隔大小,請像這樣設置 freq:
index_period = pd.PeriodIndex(["2020-12-25", "2020-12-26"], freq="2D")
index_period
PeriodIndex(['2020-12-25', '2020-12-26'], dtype='period[2D]', freq='2D')指定各個時間參數
您可以使用單獨的時間參數創建 PeriodIndex,而不是傳入 data ,如下所示:
idx = pd.PeriodIndex(year=[2000, 2002], month=[5,6], day=[10,11], freq="D")
idx
PeriodIndex(['2000-05-10', '2002-06-11'], dtype='period[D]', freq='D')指定 tz
要使 PeriodIndex 時區感知:
pd.PeriodIndex(["2020-12-25"], freq="D", tz="Asia/Tokyo")
PeriodIndex(['2020-12-25'], dtype='period[D]', freq='D')相關用法
- Python Pandas Period構造函數用法及代碼示例
- Python Sympy Permutation.list()用法及代碼示例
- Python Sympy Permutation.rank_nonlex()用法及代碼示例
- Python Sympy Permutation.next_lex()用法及代碼示例
- Python Sympy Permutation.is_odd()用法及代碼示例
- Python Sympy Permutation.support()用法及代碼示例
- Python Sympy Permutation.runs()用法及代碼示例
- Python Sympy Permutation.signature()用法及代碼示例
- Python Sympy Permutation.max()用法及代碼示例
- Python Sympy Permutation.mul_inv()用法及代碼示例
- Python Sympy Permutation.unrank_nonlex()用法及代碼示例
- Python Sympy Permutation.is_Empty()用法及代碼示例
- Python Sympy Permutation.is_Identity()用法及代碼示例
- Python Sympy Permutation.random()用法及代碼示例
- Python Sympy Permutation.rmul_with_af()用法及代碼示例
- Python Sympy Permutation.unrank_lex()用法及代碼示例
- Python Sympy Permutation.transpositions()用法及代碼示例
- Python Sympy Permutation.rmul()用法及代碼示例
- Python Sympy Permutation.next_trotterjohnson()用法及代碼示例
- Python Sympy Permutation.is_even()用法及代碼示例
- Python Tableau PersonalAccessTokenAuth用法及代碼示例
- Python Sympy Permutation.order()用法及代碼示例
- Python Django PercentRank用法及代碼示例
- Python Sympy Permutation.length()用法及代碼示例
- Python Sympy Permutation.unrank_trotterjohnson()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas | PeriodIndex constructor。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
