Tuesday, June 16, 2026
HomeLanguagesPython | Pandas DatetimeIndex.to_perioddelta()

Python | Pandas DatetimeIndex.to_perioddelta()

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.to_perioddelta() function calculate TimedeltaIndex of difference between index values and index converted to periodIndex at specified frequency. It is used for vectorized offsets.

Syntax: DatetimeIndex.to_perioddelta(freq)

Parameters :
freq : One of pandas’ offset strings or an Offset object. Will be inferred by default

Return : TimedeltaIndex

Example #1: Use DatetimeIndex.to_perioddelta() function to calculate the TimedeltaIndex of difference between index values and index converted to periodIndex at specified frequency




# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'S' represents secondly frequency 
didx = pd.DatetimeIndex(start ='2018-11-15 09:45:10', freq ='S', periods = 5)
  
# Print the DatetimeIndex
print(didx)


Output :

Now we want to calculate the TimedeltaIndex of difference between index values and index converted to periodIndex




# calculate the TimedeltaIndex 
# 'T' represents minute based frequency
didx.to_perioddelta('T')


Output :

As we can see in the output, the returned the difference between the index values and index converted to periodIndex.
 
Example #2: Use DatetimeIndex.to_perioddelta() function to calculate the TimedeltaIndex of difference between index values and index converted to periodIndex at specified frequency.




# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'T' represents minutely frequency 
didx = pd.DatetimeIndex(start ='2015-03-02 01:15:12', freq ='T', periods = 5)
  
# Print the DatetimeIndex
print(didx)


Output :

Now we want to calculate the TimedeltaIndex of difference between index values and index converted to periodIndex




# calculate the TimedeltaIndex 
# 'H' represents hourly frequency
didx.to_period('H')


Output :

As we can see in the output, the returned the difference between the index values and index converted to periodIndex.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS