Pandas | Period constructor
Start your free 7-days trial now!
Pandas Period(~)
constructor creates a new Period
object, which represents a specific time span or a duration.
Examples
Basic usage
To create a Period
object, simply call its constructor like so:
p = pd.Period("2020")p
Period('2020', 'A-DEC')
Here, "A-DEC"
means that the time span is set to annual, and the span ends at December. In Pandas terminology, we often say that the frequency is annual.
Helper properties
Period
objects come with numerous useful properties:
print("Starting time:", p.start_time)print("Ending time:", p.end_time)print("Number of months:", p.month)print("Number of weeks:", p.week)
Starting time: 2020-01-01 00:00:00Ending time: 2020-12-31 23:59:59.999999999Number of months: 12Number of weeks: 53
Here, the start_time
and end_time
are of type timestamp
.
Date arithmetics
We can also perform date arithmetics like so:
p = pd.Period("2020")p2 = p + 1p2
Period('2021', 'A-DEC')
Here, 1
was added to the year because, as stated above, the frequency is set to A
(annual).
Note that the starting time and ending time of p2
is:
print("Starting time:", p2.start_time)print("Ending time:", p2.end_time)
Starting time: 2021-01-01 00:00:00Ending time: 2021-01-31 23:59:59.999999999
Notice how the starting is not 2020
, which is to say that the effect of +1
is a shift rather than an expansion of the time span.
Setting freq parameter
By default, the freq
parameter is inferred from the date string you specify - the lowest time unit in the date string will be used.
For instance:
p = pd.Period("2020-12")p
Period('2020-12', 'M')
Here, we have M
(month) set as the frequency because the lowest time in the date string is a month (i.e. 12
).
Instead of inferring from the date string, we can explicitly indicate what frequency to use. We do this by passing in the freq
parameter:
p = pd.Period("2020", freq="M")p
Period('2020-01', 'M')
Here, the frequency is M
(month), but if we had not specified freq
, the frequency would have been A
(annual).
Now, the starting time and ending time captures a single month:
print("Starting time:", p.start_time)print("Ending time:", p.end_time)
Starting time: 2020-01-01 00:00:00Ending time: 2020-01-31 23:59:59.999999999
Setting a Period Index for DataFrame
You can set the period as the DataFrame's index by using the PeriodIndex
object, whose constructor takes in the exact same parameters as the constructor of Period
:
index_period = pd.PeriodIndex(["2020-12-25", "2020-12-26"], freq="D")index_period
PeriodIndex(['2020-12-25', '2020-12-26'], dtype='period[D]', freq='D')
Here, we are passing in a list of date strings to construct the PeriodIndex
. We can then use this PeriodIndex
as the index of our DataFrame:
pd.DataFrame({"A":["a","b"]}, index=index_period)
A2020-12-25 a2020-12-26 b