Skip to content

Commit

Permalink
SNOW-1669082: Add support for Resampler.asfreq
Browse files Browse the repository at this point in the history
Signed-off-by: Naren Krishna <[email protected]>
  • Loading branch information
sfc-gh-nkrishna committed Sep 17, 2024
1 parent 6554f82 commit 302e150
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Added support for `TimedeltaIndex.mean` method.
- 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 `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."
)

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)
with pytest.raises(NotImplementedError):
snow_df.resample("5s").asfreq(fill_value=2)

0 comments on commit 302e150

Please sign in to comment.