Using Timedelta and Period to create DateTime based indexes in Pandas
Real life data often consists of date and time-based records. From weather data to measuring some other metrics in big organizations they often rely on associating the observed data with some timestamp for evaluating the performance over time.
We have already discussed how to manipulate date and time in pandas, now let’s see the concepts of Timestamp, Periods and indexes created using these.
Using Timestamps :
Timestamp is the pandas equivalent of Python’s Datetime and is interchangeable with it in most cases. It denotes a specific point in time. Let’s see how to create Timestamp.
Timestamps alone is not very useful, until we create an index out of it. The index that we create using the Timestamps are of
Now we will see the type of dataframe index which is made up of individual timestamps.
As we can clearly see in the output, the type of the index of our dataframe is ‘DatetimeIndex’.
Using Periods : Unlike Timestamp which represents a point in time, Periods represents a period of time. It could be a month, day, year, hour etc.. Let’s see how to create Periods in Pandas.
The ‘M’ in the output represents month.
Period objects alone is not very useful until it is used as an Index in a Dataframe or a Series. An index made up of Periods are called
Now we will see the type of our dataframe index which is made up of individual Periods.
As we can see in the output, the index created using periods are called PeriodIndex.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course