Python | Pandas DatetimeIndex.snap()
Last Updated :
24 Dec, 2018
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.snap()
function is used to snap time stamps to nearest occurring frequency. The function takes a single parameter which is the frequency that we want to be applied while snapping the timestamp values of the DatetimeIndex object.
Syntax: DatetimeIndex.snap(freq)
Parameters :
freq : frequency
Return : DatetimeIndex
Example #1: Use DatetimeIndex.snap()
function to convert the given DatetimeIndex object to the nearest occurring frequency based on the input frequency.
import pandas as pd
didx = pd.DatetimeIndex(start = '2000-01-15 08:00' , freq = 'Q' ,
periods = 4 , tz = 'Asia/Calcutta' )
print (didx)
|
Output :
Now we want to convert the given DatetimeIndex object timestamp values to the nearest frequency based on the input.
Output :
As we can see in the output, the function has snapped each timestamp value in the given DatetimeIndex object.
Example #2: Use DatetimeIndex.snap()
function to convert the given DatetimeIndex object to the nearest occurring frequency based on the input frequency.
import pandas as pd
didx = pd.date_range(pd.Timestamp( "2000-01-15 08:00" ),
periods = 5 , freq = 'MS' )
print (didx)
|
Output :
Now we want to convert the given DatetimeIndex object timestamp values to the nearest frequency based on the input.
Output :
As we can see in the output, the function has snapped each timestamp value in the given DatetimeIndex object.
Share your thoughts in the comments
Please Login to comment...