Python | Pandas Series.is_monotonic

Python Methods and Functions

Pandas Series — it is a one-dimensional array with axis labels. Tags do not have to be unique, but must be hashable. The object supports indexing based on integers and labels and provides many methods for performing index-related operations.

Series.is_monotonic Pandas Series.is_monotonic returns boolean. It returns True if the data in the given Series object is monotonically increasing, otherwise it returns False .

Syntax: Series.is_monotonic

Parameter: None

Returns: boolean

Example # 1: Use the Series.is_monotonic attribute to check if the underlying data in a given Series object is monotonically increasing.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 'New York' , ' Chicago' , 'Toronto' , 'Lisbon' ])

  
# Create row axis labels

sr.index = [ 'City 1' , ' City 2' , 'City 3' , 'City 4'

  
# Print series

print (sr )

Output:

Now we will We use the Series.is_monotonic attribute to check if the underlying data in a given Series object is monotonically increasing.

# check if monotonically increases
sr.is_monotonic

Output:

As we can see from the output, the Series.is_monotonic attribute returned False indicating that the underlying object in this series object does not grow monotonically.

Example # 2: Use the Series.is_monotonic attribute to check if the underlying data in a given Series object is monotonically increasing.

# import pandas as pd

import pandas as pd

 
# Create a series

sr = pd.Series ([ '1 / 1 / 2018' , '2/1 / 2018' , '3/1 / 2018' , ' 4/1/2018 ' ])

  
# Create row axis labels

sr.index = [ ' Day 1' , 'Day 2' , 'Day 3' 'Day 4' ]

  
# Print series

print (sr)

Output:

We will now use the Series.is_monotonic attribute to check if whether the underlying data in the given Series object is monotonously increasing.

# check if monotonically increasing
sr.is_monotonic

Output:

As we can see in the output, the attribute Series.is_monotonic returned True indicating that the base object in this series object is monotonically increasing.





Tutorials