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

SNOW-1669082: Add support for Resampler.asfreq #2310

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added support for some cases of aggregating `Timedelta` columns on `axis=0` with `agg` or `aggregate`.
- Added support for `by`, `left_by`, `right_by`, `left_index`, and `right_index` for `pd.merge_asof`.
- Added support for passing parameter `include_describe` to `Session.query_history`.
- Added support for `Resampler.asfreq`.

#### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion docs/source/modin/supported/resampling_supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Upsampling
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
| Resampler method | Snowpark implemented? (Y/N/P/D) | Missing parameters | Notes for current implementation |
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
| ``asfreq`` | N | | |
| ``asfreq`` | P | ``fill_value`` | |
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
| ``bfill`` | P | ``limit`` | |
+-----------------------------+---------------------------------+----------------------------------+----------------------------------------------------+
Expand Down
20 changes: 18 additions & 2 deletions src/snowflake/snowpark/modin/pandas/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,24 @@ def fillna(self, method: str, limit: Optional[int] = None):
)
return getattr(self, method)(limit=limit)

def asfreq(self, fill_value: Optional[Any] = None): # pragma: no cover
self._method_not_implemented("asfreq")
def asfreq(self, fill_value: Optional[Any] = None):
is_series = not self._dataframe._is_dataframe

if fill_value is not None:
# TODO: SNOW-1660802: Implement `fill_value` parameter once `GroupBy.fillna` is supported
ErrorMessage.not_implemented(
"Parameter fill_value of resample.asfreq has not been implemented."
)
sfc-gh-nkrishna marked this conversation as resolved.
Show resolved Hide resolved

return self._dataframe.__constructor__(
query_compiler=self._query_compiler.resample(
self.resample_kwargs,
"first",
(),
{},
is_series,
)
)

def interpolate(
self,
Expand Down
39 changes: 38 additions & 1 deletion src/snowflake/snowpark/modin/plugin/docstrings/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,44 @@ def fillna():
"""

def asfreq():
pass
"""
Return the values at the new freq, essentially a reindex.

Parameters
----------
fill_value : scalar, optional
This parameter is not supported and will raise NotImplementedError.

Returns
-------
:class:`~modin.pandas.Series` or :class:`~modin.pandas.DataFrame`
Values at the specified freq.

See Also
--------
Series.asfreq: Convert TimeSeries to specified frequency.
DataFrame.asfreq: Convert TimeSeries to specified frequency.

Examples
--------
>>> s = pd.Series([1, 2, 3], index=pd.date_range('20180101', periods=3, freq='h'))
>>> s
2018-01-01 00:00:00 1
2018-01-01 01:00:00 2
2018-01-01 02:00:00 3
Freq: None, dtype: int64
>>> s.resample("30min").asfreq()
2018-01-01 00:00:00 1.0
2018-01-01 00:30:00 NaN
2018-01-01 01:00:00 2.0
2018-01-01 01:30:00 NaN
2018-01-01 02:00:00 3.0
Freq: None, dtype: float64
>>> s.resample("2h").asfreq()
2018-01-01 00:00:00 1
2018-01-01 02:00:00 3
Freq: None, dtype: int64
"""

def interpolate():
pass
Expand Down
15 changes: 15 additions & 0 deletions tests/integ/modin/resample/test_resample_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ def test_asfreq_ffill():
)


@sql_count_checker(query_count=3, join_count=1)
@pytest.mark.parametrize("freq", ["3min", "30S"])
def test_resampler_asfreq(freq):
eval_snowpark_pandas_result(
*create_test_dfs(
{"A": np.random.randn(15)},
index=native_pd.date_range("2020-01-01", periods=15, freq="1min"),
),
lambda df: df.resample(freq).asfreq(),
check_freq=False,
)


@sql_count_checker(query_count=0)
def test_asfreq_negative():
snow_df = pd.DataFrame(
Expand All @@ -56,3 +69,5 @@ def test_asfreq_negative():
snow_df.asfreq(freq="5s", method="ffill", normalize=True)
with pytest.raises(NotImplementedError):
snow_df.asfreq(freq="5s", method="ffill", fill_value=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we testing DataFrame.asfreq here when the test file is test_resample_asfreq?

Copy link
Contributor Author

@sfc-gh-nkrishna sfc-gh-nkrishna Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataFrame/Series.asfreq ends up just calling the resample implementation with the first aggregation. I didn't realize at that time the Resampler class also has an asfreq so this PR just adds that. That's why all 3 are tested in the same file under the resample folder.

with pytest.raises(NotImplementedError):
snow_df.resample("5s").asfreq(fill_value=2)
Loading