我无法理解 PeriodIndex 和 DateTimeIndex 之间的区别,以及何时使用它们。特别是,对我来说,使用 Periods 似乎总是比使用 Timestamps 更自然,但最近我发现 Timestamps 似乎提供了相同的索引功能,可以与 timegrouper 一起使用,并且还可以更好地与 Matplotlib 的日期功能一起使用。所以我想知道是否有充分的理由使用 Periods(PeriodIndex)?
最佳答案
周期可用于检查特定事件是否在特定周期内发生。基本上,Period 代表一个时间间隔,而 Timestamp 代表一个时间点。
# For example, this will return True since the period is 1Day. This test cannot be done with a Timestamp.
p = pd.Period('2017-06-13')
test = pd.Timestamp('2017-06-13 22:11')
p.start_time < test < p.end_time
我认为人们使用期间/时间戳的最简单原因是他/她的代码是否需要来自期间和时间戳的属性。
关于Python - Pandas - 时间戳和周期范围之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42284591/