Python Pandas - 获取区间的正确界限
要获得区间的正确界限,请使用interval.right 属性。首先,导入所需的库-
import pandas as pd
使用时间戳作为边界来创建时间间隔。使用值为“right”的“closed”参数设置的闭合区间-
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 #UseTimestampsastheboundstocreateatimeinterval # Closed interval set using the "closed" 带值的参数 "right" interval = pd.Interval(pd.Timestamp('2020-01-01 00:00:00'), pd.Timestamp('2021-01-01 00:00:00'), closed='left') #displaytheinterval print("Interval...\n",interval) #displaytheinterval length print("\nInterval length...\n",interval.length) #gettherightbound 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