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