Python Pandas - 獲得區間右側邊界


要獲得區間右側邊界,請使用 interval.right 屬性。首先,匯入必需的庫 −

import pandas as pd

使用時間戳作為邊界來建立時間間隔。閉區間使用 "closed" 引數,值為 "right" −

interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'),
   pd.Timestamp('2021-01-01 00:00:00'), closed='left')

獲取右側邊界 −

print("\nThe right bound for the Interval...\n",interval.right)

示例

以下為程式碼 −

import pandas as pd

# Use Timestamps as the bounds to create a time interval
# Closed interval set using the "closed" parameter with value "right"
interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'),
   pd.Timestamp('2021-01-01 00:00:00'), closed='left')

# display the interval
print("Interval...\n",interval)

# display the interval length
print("\nInterval length...\n",interval.length)

# get the right bound
print("\nThe right bound for the Interval...\n",interval.right)

輸出

這將生成以下程式碼 −

Interval...
[2020-01-01, 2021-01-01)
Interval length...
366 days 00:00:00
The right bound for the Interval...
2021-01-01 00:00:00

更新於: 2021-10-12

797 次瀏覽

啟動你的 職業

完成本課程即可獲得認證

開始學習
廣告
© . All rights reserved.