Skip to content

Commit

Permalink
Merge branch 'main' into rdurrani-SNOW-1502893
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-rdurrani authored Aug 30, 2024
2 parents 796445a + e1149ca commit 5c19d5b
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 48 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
- Added support for `DatetimeIndex.round`, `DatetimeIndex.floor` and `DatetimeIndex.ceil`.
- Added support for `Series.dt.days_in_month` and `Series.dt.daysinmonth`.
- Added support for `DataFrameGroupBy.value_counts` and `SeriesGroupBy.value_counts`.
- Added support for `Series.is_monotonic_increasing` and `Series.is_monotonic_decreasing`.
- Added support for `Index.is_monotonic_increasing` and `Index.is_monotonic_decreasing`.
- Added support for `pd.crosstab`.

#### Improvements
Expand Down
2 changes: 2 additions & 0 deletions docs/source/modin/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Series
Series.equals
Series.empty
Series.hasnans
Series.is_monotonic_increasing
Series.is_monotonic_decreasing
Series.name
Series.ndim
Series.shape
Expand Down
4 changes: 2 additions & 2 deletions docs/source/modin/supported/index_supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Attributes
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``values`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``is_monotonic_increasing`` | N | |
| ``is_monotonic_increasing`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``is_monotonic_decreasing`` | N | |
| ``is_monotonic_decreasing`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``is_unique`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
Expand Down
4 changes: 2 additions & 2 deletions docs/source/modin/supported/series_supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Attributes
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``index`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``is_monotonic_decreasing`` | N | |
| ``is_monotonic_decreasing`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``is_monotonic_increasing`` | N | |
| ``is_monotonic_increasing`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
| ``is_unique`` | Y | |
+-----------------------------+---------------------------------+----------------------------------------------------+
Expand Down
128 changes: 106 additions & 22 deletions src/snowflake/snowpark/modin/plugin/compiler/snowflake_query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2286,9 +2286,82 @@ def reindex(
else:
return self._reindex_axis_1(labels=labels, **kwargs)

def is_monotonic_decreasing(self) -> "SnowflakeQueryCompiler":
"""
Returns a QueryCompiler containing only a column that checks for monotonically
decreasing values in the first data column of this QueryCompiler.

Returns
-------
SnowflakeQueryCompiler
QueryCompiler with column to ascertain whether data is monotonically decreasing.
"""
return self._check_monotonic(increasing=False)

def is_monotonic_increasing(self) -> "SnowflakeQueryCompiler":
"""
Returns a QueryCompiler containing only a column that checks for monotonically
increasing values in the first data column of this QueryCompiler.

Returns
-------
SnowflakeQueryCompiler
QueryCompiler with column to ascertain whether data is monotonically increasing.
"""
return self._check_monotonic(increasing=True)

def _check_monotonic(self, increasing: bool) -> "SnowflakeQueryCompiler":
"""
Returns a QueryCompiler containing only a column that checks for monotonically
decreasing or increasing values (depending on `increasing`) in the first data column of this QueryCompiler.

Parameters
----------
increasing: bool
Whether to check for monotonically increasing or decreasing values.

Returns
-------
SnowflakeQueryCompiler
QueryCompiler with column to ascertain whether data is monotonically decreasing/increasing.
"""
col_to_check = self._modin_frame.data_column_snowflake_quoted_identifiers[0]
(
new_qc,
monotonic_increasing_snowflake_quoted_identifier,
monotonic_decreasing_snowflake_quoted_identifier,
) = self._add_columns_for_monotonicity_checks(
col_to_check=col_to_check,
columns_to_add="increasing" if increasing else "decreasing",
)
data_column_snowflake_quoted_identifiers = []
if increasing:
data_column_snowflake_quoted_identifiers.append(
monotonic_increasing_snowflake_quoted_identifier
)
else:
data_column_snowflake_quoted_identifiers.append(
monotonic_decreasing_snowflake_quoted_identifier
)
new_modin_frame = new_qc._modin_frame
return SnowflakeQueryCompiler(
InternalFrame.create(
ordered_dataframe=new_modin_frame.ordered_dataframe.limit(
n=1, sort=False
),
data_column_pandas_index_names=new_modin_frame.data_column_pandas_index_names,
data_column_pandas_labels=["monotonic_column"],
data_column_snowflake_quoted_identifiers=data_column_snowflake_quoted_identifiers,
index_column_pandas_labels=new_modin_frame.index_column_pandas_labels,
index_column_snowflake_quoted_identifiers=new_modin_frame.index_column_snowflake_quoted_identifiers,
data_column_types=None,
index_column_types=None,
)
)

def _add_columns_for_monotonicity_checks(
self, col_to_check: str
) -> tuple["SnowflakeQueryCompiler", str, str]:
self, col_to_check: str, columns_to_add: Optional[str] = None
) -> tuple["SnowflakeQueryCompiler", Optional[str], Optional[str]]:
"""
Adds columns that check for monotonicity (increasing or decreasing) in the
specified column.
Expand All @@ -2297,6 +2370,8 @@ def _add_columns_for_monotonicity_checks(
----------
col_to_check : str
The Snowflake quoted identifier for the column whose monotonicity to check.
columns_to_add : str, optional
Whether or not to add all columns, and if not, which columns to add.

Returns
-------
Expand All @@ -2307,36 +2382,45 @@ def _add_columns_for_monotonicity_checks(
"""
self._raise_not_implemented_error_for_timedelta()

assert columns_to_add in [
None,
"increasing",
"decreasing",
], "Invalid value passed to function"
modin_frame = self._modin_frame
modin_frame = modin_frame.ensure_row_position_column()
row_position_column = modin_frame.row_position_snowflake_quoted_identifier
monotonic_decreasing_snowflake_quoted_id = None
monotonic_increasing_snowflake_quoted_id = None
modin_frame = modin_frame.append_column(
"_index_lag_col",
lag(col_to_check).over(Window.order_by(row_position_column)),
)
lag_col_snowflake_quoted_id = (
modin_frame.data_column_snowflake_quoted_identifiers[-1]
)
modin_frame = modin_frame.append_column(
"_is_monotonic_decreasing",
coalesce(
min_(col(col_to_check) < col(lag_col_snowflake_quoted_id)).over(),
pandas_lit(False),
),
)
monotonic_decreasing_snowflake_quoted_id = (
modin_frame.data_column_snowflake_quoted_identifiers[-1]
)
modin_frame = modin_frame.append_column(
"_is_monotonic_increasing",
coalesce(
min_(col(col_to_check) > col(lag_col_snowflake_quoted_id)).over(),
pandas_lit(False),
),
)
monotonic_increasing_snowflake_quoted_id = (
modin_frame.data_column_snowflake_quoted_identifiers[-1]
)
if columns_to_add in [None, "decreasing"]:
modin_frame = modin_frame.append_column(
"_is_monotonic_decreasing",
coalesce(
min_(col(col_to_check) <= col(lag_col_snowflake_quoted_id)).over(),
pandas_lit(False),
),
)
monotonic_decreasing_snowflake_quoted_id = (
modin_frame.data_column_snowflake_quoted_identifiers[-1]
)
if columns_to_add in [None, "increasing"]:
modin_frame = modin_frame.append_column(
"_is_monotonic_increasing",
coalesce(
min_(col(col_to_check) >= col(lag_col_snowflake_quoted_id)).over(),
pandas_lit(False),
),
)
monotonic_increasing_snowflake_quoted_id = (
modin_frame.data_column_snowflake_quoted_identifiers[-1]
)
data_column_pandas_labels = modin_frame.data_column_pandas_labels
data_column_snowflake_quoted_identifiers = (
modin_frame.data_column_snowflake_quoted_identifiers
Expand Down
54 changes: 42 additions & 12 deletions src/snowflake/snowpark/modin/plugin/docstrings/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3661,6 +3661,48 @@ def hasnans():
Return True if there are any NaNs.
"""

@property
def is_monotonic_decreasing():
"""
Return boolean if values in the object are monotonically decreasing.
Returns
-------
bool
Whether or not the Series is monotonically decreasing.
Examples
--------
>>> s = pd.Series([3, 2, 2, 1])
>>> s.is_monotonic_decreasing
True
>>> s = pd.Series([1, 2, 3])
>>> s.is_monotonic_decreasing
False
"""

@property
def is_monotonic_increasing():
"""
Return boolean if values in the object are monotonically increasing.
Returns
-------
bool
Whether or not the Series is monotonically increasing.
Examples
--------
>>> s = pd.Series([1, 2, 2])
>>> s.is_monotonic_increasing
True
>>> s = pd.Series([3, 2, 1])
>>> s.is_monotonic_increasing
False
"""

def isna():
"""
Detect missing values.
Expand Down Expand Up @@ -3721,18 +3763,6 @@ def isnull():
dtype: bool
"""

@property
def is_monotonic_increasing():
"""
Return True if values in the Series are monotonic_increasing.
"""

@property
def is_monotonic_decreasing():
"""
Return True if values in the Series are monotonic_decreasing.
"""

@property
def is_unique():
"""
Expand Down
28 changes: 22 additions & 6 deletions src/snowflake/snowpark/modin/plugin/extensions/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ def values(self) -> ArrayLike:
return self.to_pandas().values

@property
@index_not_implemented()
def is_monotonic_increasing(self) -> None:
def is_monotonic_increasing(self) -> bool:
"""
Return a boolean if the values are equal or increasing.
Expand All @@ -411,12 +410,20 @@ def is_monotonic_increasing(self) -> None:
See Also
--------
Index.is_monotonic_decreasing : Check if the values are equal or decreasing
Examples
--------
>>> pd.Index([1, 2, 3]).is_monotonic_increasing
True
>>> pd.Index([1, 2, 2]).is_monotonic_increasing
True
>>> pd.Index([1, 3, 2]).is_monotonic_increasing
False
"""
# TODO: SNOW-1458134 implement is_monotonic_increasing
return self.to_series().is_monotonic_increasing

@property
@index_not_implemented()
def is_monotonic_decreasing(self) -> None:
def is_monotonic_decreasing(self) -> bool:
"""
Return a boolean if the values are equal or decreasing.
Expand All @@ -428,8 +435,17 @@ def is_monotonic_decreasing(self) -> None:
See Also
--------
Index.is_monotonic_increasing : Check if the values are equal or increasing
Examples
--------
>>> pd.Index([3, 2, 1]).is_monotonic_decreasing
True
>>> pd.Index([3, 2, 2]).is_monotonic_decreasing
True
>>> pd.Index([3, 1, 2]).is_monotonic_decreasing
False
"""
# TODO: SNOW-1458134 implement is_monotonic_decreasing
return self.to_series().is_monotonic_decreasing

@property
def is_unique(self) -> bool:
Expand Down
Loading

0 comments on commit 5c19d5b

Please sign in to comment.