Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Pandas - Difference between timestamps and period range

Tags:

python

pandas

I am having troubles understanding the difference between a PeriodIndex and a DateTimeIndex, and when to use which. 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. So I am wondering if there is every a reason to use Periods (a PeriodIndex)?

like image 933
clog14 Avatar asked Feb 16 '17 21:02

clog14


1 Answers

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.

# 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.

like image 139
clumdee Avatar answered Oct 20 '22 10:10

clumdee