Python | Pandas TimedeltaIndex.contains

Python | Pandas TimedeltaIndex.contains

The contains method of a TimedeltaIndex object in pandas checks if a specified timedelta value exists within the index.

Let's say you have a TimedeltaIndex, and you want to check if a specific timedelta value is present in that index. You can use the contains method for this.

Here's an example to illustrate its usage:

import pandas as pd # Create a TimedeltaIndex td_index = pd.TimedeltaIndex(['1 days', '2 days', '3 days', '4 days']) # Check if a specific timedelta is in the index contains_2days = td_index.contains(pd.Timedelta('2 days')) contains_5days = td_index.contains(pd.Timedelta('5 days')) print(f"Contains 2 days: {contains_2days}") # True print(f"Contains 5 days: {contains_5days}") # False 

In the above example, the TimedeltaIndex has a value of '2 days', so contains returns True for that. However, it does not have a value of '5 days', so contains returns False for that.

Remember to provide the timedelta value you are checking for as a pd.Timedelta object to the contains method.


More Tags

android-databinding jmeter-4.0 streamwriter collocation pascals-triangle circular-dependency dynamic-memory-allocation delicious-api mean mariadb

More Programming Guides

Other Guides

More Programming Examples