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.



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output :

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# find the frequency of the object.
didx.inferred_freq

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output :

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# find the frequency of the object.
didx.inferred_freq

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.