简体   繁体   English

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

[英]Python - Pandas - Difference between timestamps and period range

I am having troubles understanding the difference between a PeriodIndex and a DateTimeIndex, and when to use which. 我很难理解PeriodIndex和DateTimeIndex之间的区别,以及何时使用它。 In particular, it always seemed to be more natural to me to use Periods as opposed to Timestamps, but recently I discovered that Timestamps seem to provide the same indexing capability, can be used with the timegrouper and also work better with Matplotlib's date functionalities. 特别是,使用Periods而不是Timestamps似乎更自然,但最近我发现Timestamps似乎提供了相同的索引功能,可以与timegrouper一起使用,并且还可以更好地使用Matplotlib的日期功能。 So I am wondering if there is every a reason to use Periods (a PeriodIndex)? 所以我想知道是否有任何理由使用Periods(PeriodIndex)?

Periods can be use to check if a specific event occurs within a certain period. 期间可用于检查特定事件是否在特定时期内发生。 Basically a Period represents an interval while a Timestamp represents a point in time. 基本上,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

I believe the simplest reason for ones to use Periods/Timestamps is whether attributes from a Period and a Timestamp are needed for his/her code. 我认为使用Periods / Timestamps的最简单的原因是他/她的代码是否需要来自Period和Timestamp的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM