Wednesday, July 3, 2024
HomeLanguagesPythonPython | Pandas DatetimeIndex.inferred_freq

Python | Pandas DatetimeIndex.inferred_freq

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas DatetimeIndex.inferred_freq attribute tries to return a string representing a frequency guess, generated by infer_freq. For those cases in which the function is not able to auto detect the frequency of the DatetimeIndex it returns None.

Syntax: DatetimeIndex.inferred_freq

Return: freq

Example #1: Use DatetimeIndex.inferred_freq attribute to auto detect the frequency of the given DatetimeIndex object.




# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
didx = pd.date_range("2008-12-30", periods = 5, freq ='Q')
  
# Print the DatetimeIndex
print(didx)


Output :

Now we want the function to auto detect the frequency of the given DatetimeIndex object.




# find the frequency of the object.
didx.inferred_freq


Output :

As we can see in the output, the function has tried to auto detect the frequency of the given DatetimeIndex object and has returned a quarter type frequency starting from the month of December.
 
Example #2: Use DatetimeIndex.inferred_freq attribute to auto detect the frequency of the given DatetimeIndex object.




# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
didx = pd.DatetimeIndex(start ='2000-01-31 06:30', freq ='BM'
                           periods = 5, tz ='Asia/Calcutta')
  
# Print the DatetimeIndex
print(didx)


Output :

Now we want the function to auto detect the frequency of the given DatetimeIndex object.




# find the frequency of the object.
didx.inferred_freq


Output :

As we can see in the output, the function has tried to auto detect the frequency of the given DatetimeIndex object and has returned ‘BM’ (Business Month end) frequency.

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments