Python - Pandas - 时间戳和周期范围之间的差异

时间:2017-02-16 21:05:14

标签: python pandas

我很难理解PeriodIndex和DateTimeIndex之间的区别,以及何时使用哪个。特别是,对于我来说,使用Periods而不是Timestamps似乎更自然,但最近我发现Timestamps似乎提供了相同的索引功能,可以与timegrouper一起使用,也可以更好地与Matplotlib一起使用日期功能。所以我想知道是否有任何理由使用Periods(PeriodIndex)?

1 个答案:

答案 0 :(得分:12)

期间可用于检查特定事件是否在特定时间段内发生。基本上,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

我认为使用Periods / Timestamps的最简单的原因是他/她的代码是否需要来自Period和Timestamp的属性。