Open In App

Python | Pandas TimedeltaIndex.get_slice_bound

Last Updated : 28 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
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 TimedeltaIndex.get_slice_bound() function calculate slice bound that corresponds to given label. The function return leftmost (one-past-the-rightmost if side==’right’) position of given label.

Syntax : TimedeltaIndex.get_slice_bound(label, side, kind)

Parameters :
label : object
side : {‘left’, ‘right’}
kind : {‘ix’, ‘loc’, ‘getitem’}

Return : int

Example #1: Use TimedeltaIndex.get_slice_bound() function to find the left slice bound of a value in the given TimedeltaIndex object.




# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(data =['3 days 06:05:01.000030', '1 days 06:05:01.000030',
                                '3 days 06:05:01.000030', '1 days 02:00:00',
                                                 '21 days 06:15:01.000030'])
  
# Print the TimedeltaIndex object
print(tidx)


Output :

Now we will use the TimedeltaIndex.get_slice_bound() function to find the left slice bound of ‘1 days 02:00:00’.




# find the left slice bound for passed value
tidx.get_slice_bound('1 days 02:00:00', side ='left', kind ='loc')


Output :

As we can see in the output, the TimedeltaIndex.get_slice_bound() function has returned 3 as the slice bound for the passed value in the given TimedeltaIndex object.
 
Example #2: Use TimedeltaIndex.get_slice_bound() function to find the left slice bound of a value in the given TimedeltaIndex object.




# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(data =['06:05:01.000030', '3 days 06:05:01.000030'
                                '22 day 2 min 3us 10ns', '+23:59:59.999999',
                             '13 days 06:05:01.000030', '+12:19:59.999999'])
  
# Print the TimedeltaIndex object
print(tidx)


Output :

Now we will use the TimedeltaIndex.get_slice_bound() function to find the left slice bound of ’13 days 06:05:01.000030′.




# find the left slice bound for passed value
tidx.get_slice_bound('13 days 06:05:01.000030', side ='left', kind ='loc')


Output :

As we can see in the output, the TimedeltaIndex.get_slice_bound() function has returned 4 as the slice bound for the passed value in the given TimedeltaIndex object.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads