Python | Pandas TimedeltaIndex.is_monotonic

Python | Pandas TimedeltaIndex.is_monotonic

The TimedeltaIndex.is_monotonic property in pandas indicates whether a TimedeltaIndex is monotonically increasing (from start to end). If the TimedeltaIndex is either increasing or constant across all elements, then is_monotonic will return True. If there's any decrease from one element to the next, it will return False.

Example:

import pandas as pd # Monotonically increasing TimedeltaIndex index1 = pd.to_timedelta(['1 days', '2 days', '3 days', '3 days', '4 days']) print(index1.is_monotonic) # True # Not a monotonically increasing TimedeltaIndex index2 = pd.to_timedelta(['1 days', '3 days', '3 days', '2 days', '4 days']) print(index2.is_monotonic) # False 

In the example above:

  • index1 is monotonically increasing. Even though there are two consecutive '3 days', it's still considered monotonic because the index doesn't decrease.
  • index2 is not monotonically increasing because it goes from '3 days' to '2 days', which is a decrease.

This property can be useful when checking the order of data or when certain algorithms or operations require the index to be sorted in a particular manner.


More Tags

punctuation hosts uitabbarcontroller tld image-resizing bezier popup-blocker enumeration local-variables mongorestore

More Programming Guides

Other Guides

More Programming Examples