Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a column in pandas.DF() monotonically increasing?

I can check if the index of a pandas.DataFrame() is monotonically increasing by using is_monotonic method. However, I would like to check if one of the column value is strictly increasing in value(float/integer) ?

In [13]: my_df = pd.DataFrame([1,2,3,5,7,6,9])

In [14]: my_df
Out[14]: 
   0
0  1
1  2
2  3
3  5
4  7
5  6
6  9

In [15]: my_df.index.is_monotonic
Out[15]: True
like image 977
amehta Avatar asked Jan 22 '15 16:01

amehta


People also ask

Are pandas monotonic?

is_monotonic attribute return a boolean value. It returns True if the data in the given Series object is monotonically increasing else it return False . Example #1: Use Series.

How do you check if a list is monotonically increasing python?

We can check each of these properties in one pass. To check whether an array A is monotone increasing, we'll check A[i] <= A[i+1] for all i indexing from 0 to len(A)-2. Similarly we can check for monotone decreasing where A[i] >= A[i+1] for all i indexing from 0 to len(A)-2.

How do you know if a series is monotonic?

If a sequence is monotonic, it means that it's always increasing or always decreasing. If a sequence is sometimes increasing and sometimes decreasing and therefore doesn't have a consistent direction, it means that the sequence is not monotonic.

What does DataFrame DF columns give?

It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas. Pandas DataFrame. columns attribute return the column labels of the given Dataframe.


2 Answers

Pandas 0.19 added a public Series.is_monotonic API (previously, this was available only in the undocumented algos module).

(Updated) Note that despite its name, Series.is_monotonic only indicates whether a series is monotonically increasing (equivalent to using Series.is_monotonic_increasing). For the other way around, use Series.is_monotonic_decreasing. Anyway, both are non-strict, but you can combine them with is_unqiue to get strictness.

e.g.:

my_df = pd.DataFrame([1,2,2,3], columns = ['A'])

my_df['A'].is_monotonic    # non-strict
Out[1]: True

my_df['A'].is_monotonic_increasing    # equivalent to is_monotonic
Out[2]: True

(my_df['A'].is_monotonic_increasing and my_df['A'].is_unique)    # strict  
Out[3]: False

my_df['A'].is_monotonic_decreasing    # Other direction (also non-strict)
Out[4]: False

You can use apply to run this at a DataFrame level:

my_df = pd.DataFrame({'A':[1,2,3],'B':[1,1,1],'C':[3,2,1]})
my_df
Out[32]: 
   A  B  C
0  1  1  3
1  2  1  2
2  3  1  1

my_df.apply(lambda x: x.is_monotonic)
Out[33]: 
A     True
B     True
C    False
dtype: bool
like image 86
OmerB Avatar answered Sep 23 '22 23:09

OmerB


Probably the best way is to obtain a dataframe column as a numpy array without copying data around (using the .values property after column selection via indexing), and to then use a numpy-based test for checking monotonicity:

def monotonic(x):
    return np.all(np.diff(x) > 0)

monotonic(df[0].values)

A pure Python implementation, borrowed from here: Python - How to check list monotonicity

def strictly_increasing(L):
    return all(x<y for x, y in zip(L, L[1:]))
like image 5
Dr. Jan-Philip Gehrcke Avatar answered Sep 25 '22 23:09

Dr. Jan-Philip Gehrcke