Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make is_monotonic~ work properly for index #930

Merged
merged 9 commits into from
Oct 22, 2019
20 changes: 18 additions & 2 deletions databricks/koalas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from databricks.koalas.internal import _InternalFrame
from databricks.koalas.typedef import pandas_wraps, spark_type_to_pandas_dtype
from databricks.koalas.utils import align_diff_series, scol_for
from databricks.koalas.internal import SPARK_INDEX_NAME_FORMAT
itholic marked this conversation as resolved.
Show resolved Hide resolved
from pyspark.sql.functions import monotonically_increasing_id
itholic marked this conversation as resolved.
Show resolved Hide resolved


def _column_op(f):
Expand Down Expand Up @@ -299,9 +301,16 @@ def is_monotonic(self):

>>> ser.rename("a").to_frame().set_index("a").index.is_monotonic
True

>>> ser = ks.Series([5, 4, 3, 2, 1], index=[1, 2, 3, 4, 5])
>>> ser.is_monotonic
False

>>> ser.index.is_monotonic
True
"""
col = self._scol
window = Window.orderBy(self._kdf._internal.index_scols).rowsBetween(-1, -1)
itholic marked this conversation as resolved.
Show resolved Hide resolved
window = Window.orderBy(monotonically_increasing_id()).rowsBetween(-1, -1)
return self._with_new_scol((col >= F.lag(col, 1).over(window)) & col.isNotNull()).all()

is_monotonic_increasing = is_monotonic
Expand Down Expand Up @@ -343,9 +352,16 @@ def is_monotonic_decreasing(self):

>>> ser.rename("a").to_frame().set_index("a").index.is_monotonic_decreasing
True

>>> ser = ks.Series([5, 4, 3, 2, 1], index=[1, 2, 3, 4, 5])
>>> ser.is_monotonic_decreasing
True

>>> ser.index.is_monotonic_decreasing
False
"""
col = self._scol
window = Window.orderBy(self._kdf._internal.index_scols).rowsBetween(-1, -1)
window = Window.orderBy(monotonically_increasing_id()).rowsBetween(-1, -1)
return self._with_new_scol((col <= F.lag(col, 1).over(window)) & col.isNotNull()).all()

def astype(self, dtype):
Expand Down