search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas | Period constructor

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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:00
Ending time: 2020-12-31 23:59:59.999999999
Number of months: 12
Number 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 + 1
p2
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:00
Ending 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:00
Ending 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)
A
2020-12-25 a
2020-12-26 b
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!