pandas PeriodIndex:freq not specified and cannot be inferred from first element

Published on:
Last updated:

This post is also available in: 日本語 (Japanese)

This post is about how to solve the followings error when handling time series data.

ValueError freq not specified and cannot be inferred from first element

It seems that the solution needs to specify "freq" of the time series data clearly.
So, here is some sample code to convert a pandas dataframe from DatetimeIndex to PeriodIndex.

import pandas as pd
from datetime import datetime, timedelta

# Create time series data 
date_list = [datetime(2021,1,1) + timedelta(days=i) for i in range(100)]
print(date_list)
"""
#output
[datetime.datetime(2021, 1, 1, 0, 0), datetime.datetime(2021, 1, 2, 0, 0),...]
"""

#Create dataframe
#from date_list, create index and columns
df_date = pd.DataFrame(date_list, columns=['Date'], index=date_list)
print(df_date)
"""
#output
                 Date
2021-01-01 2021-01-01
2021-01-02 2021-01-02
2021-01-03 2021-01-03
2021-01-04 2021-01-04
2021-01-05 2021-01-05
...               ...
2021-04-06 2021-04-06
2021-04-07 2021-04-07
2021-04-08 2021-04-08
2021-04-09 2021-04-09
2021-04-10 2021-04-10
"""

print(type(df_date.index)) #<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
print(type(df_date["Date"][0])) #<class 'pandas._libs.tslibs.timestamps.Timestamp'>

#Convert to PeriodIndex
#freq="B" means from Monday to Friday.
df_date.index = df_date.index.to_period(freq="B")
df_date["Date"] = df_date["Date"].dt.to_period(freq="B")
print(df_date)
"""
#output

                  Date
2021-01-01  2021-01-01
2021-01-04  2021-01-04
2021-01-04  2021-01-04
2021-01-04  2021-01-04
2021-01-05  2021-01-05
...                ...
2021-04-06  2021-04-06
2021-04-07  2021-04-07
2021-04-08  2021-04-08
2021-04-09  2021-04-09
2021-04-12  2021-04-12
"""

print(type(df_date.index)) #<class 'pandas.core.indexes.period.PeriodIndex'>
print(type(df_date["Date"][0])) #<class 'pandas._libs.tslibs.period.Period'>

About
Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and follow me on LinkedIn.