Python | Pandas DatetimeIndex.snap() Last Updated : 24 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Python3 # importing pandas as pd import pandas as pd # Create the DatetimeIndex # Here 'Q' represents quarter end frequency didx = pd.DatetimeIndex(start ='2000-01-15 08:00', freq ='Q', periods = 4, tz ='Asia/Calcutta') # Print the DatetimeIndex print(didx) Output : Now we want to convert the given DatetimeIndex object timestamp values to the nearest frequency based on the input. Python3 # snap the timestamp to the nearest frequency didx.snap('MS') 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. Python3 # importing pandas as pd import pandas as pd # Create the DatetimeIndex # Here 'MS' represents month start frequency didx = pd.date_range(pd.Timestamp("2000-01-15 08:00"), periods = 5, freq ='MS') # Print the DatetimeIndex print(didx) Output : Now we want to convert the given DatetimeIndex object timestamp values to the nearest frequency based on the input. Python3 # snap the timestamp to the nearest frequency didx.snap('Q') Output : As we can see in the output, the function has snapped each timestamp value in the given DatetimeIndex object. Comment More infoAdvertise with us Next Article Python | Pandas DatetimeIndex.day S Shubham__Ranjan Follow Improve Article Tags : Technical Scripter Python Python-pandas Python pandas-datetimeIndex Practice Tags : python Similar Reads Python | Pandas DatetimeIndex.year 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.year attribute outputs an Index object containing the value of ye 2 min read Python | Pandas DatetimeIndex.second 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.second attribute outputs an Index object containing the second va 2 min read Python | Pandas DatetimeIndex.time 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.time attribute outputs an Index object containing the time values 2 min read Python | Pandas DatetimeIndex.quarter 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.quarter attribute outputs the quarter of the date for each entrie 2 min read Python | Pandas DatetimeIndex.day 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.day attribute outputs an Index object containing the days in each 2 min read Python | Pandas DatetimeIndex.strftime() 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.strftime() function convert to Index using specified date_format. 2 min read Like