Skip to content

Commit e04393a

Browse files
committed
raise for unsupported values
1 parent ed9be7d commit e04393a

File tree

5 files changed

+159
-69
lines changed

5 files changed

+159
-69
lines changed

bigframes/core/blocks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,26 @@ def _generate_resample_label(
19971997
f"Only offset strings are currently supported for rule, but got {repr(rule)}. {constants.FEEDBACK_LINK}"
19981998
)
19991999

2000+
if rule in ("ME", "YE", "QE", "BME", "BA", "BQE", "W"):
2001+
raise NotImplementedError(
2002+
f"Offset strings 'ME', 'YE', 'QE', 'BME', 'BA', 'BQE', 'W' are not currently supported for rule, but got {repr(rule)}. {constants.FEEDBACK_LINK}"
2003+
)
2004+
2005+
if closed == "right":
2006+
raise NotImplementedError(
2007+
f"Only closed='left' is currently supported. {constants.FEEDBACK_LINK}",
2008+
)
2009+
2010+
if label == "right":
2011+
raise NotImplementedError(
2012+
f"Only label='left' is currently supported. {constants.FEEDBACK_LINK}",
2013+
)
2014+
2015+
if origin not in ("epoch", "start", "start_day"):
2016+
raise NotImplementedError(
2017+
f"Only origin='epoch', 'start', 'start_day' are currently supported, but got {repr(origin)}. {constants.FEEDBACK_LINK}"
2018+
)
2019+
20002020
# Validate and resolve the index or column to use for grouping
20012021
if on is None:
20022022
if len(self.index_columns) == 0:

bigframes/dataframe.py

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4197,70 +4197,6 @@ def resample(
41974197
Literal["epoch", "start", "start_day", "end", "end_day"],
41984198
] = "start_day",
41994199
) -> bigframes.core.groupby.DataFrameGroupBy:
4200-
"""Resample time-series data.
4201-
4202-
**Examples:**
4203-
4204-
>>> import bigframes.pandas as bpd
4205-
>>> data = {
4206-
... "timestamp_col": pd.date_range(
4207-
... start="2021-01-01 13:00:00", periods=30, freq="1s"
4208-
... ),
4209-
... "int64_col": range(30),
4210-
... "int64_too": range(10, 40),
4211-
... }
4212-
4213-
Resample on a DataFrame with index:
4214-
4215-
>>> df = bpd.DataFrame(data).set_index("timestamp_col")
4216-
>>> df.resample(rule="7s").min()
4217-
int64_col int64_too
4218-
2021-01-01 12:59:55 0 10
4219-
2021-01-01 13:00:02 2 12
4220-
2021-01-01 13:00:09 9 19
4221-
2021-01-01 13:00:16 16 26
4222-
2021-01-01 13:00:23 23 33
4223-
<BLANKLINE>
4224-
[5 rows x 2 columns]
4225-
4226-
Resample with column and origin set to 'start':
4227-
4228-
>>> df = bpd.DataFrame(data)
4229-
>>> df.resample(rule="7s", on = "timestamp_col", origin="start").min()
4230-
int64_col int64_too
4231-
2021-01-01 13:00:00 0 10
4232-
2021-01-01 13:00:07 7 17
4233-
2021-01-01 13:00:14 14 24
4234-
2021-01-01 13:00:21 21 31
4235-
2021-01-01 13:00:28 28 38
4236-
<BLANKLINE>
4237-
[5 rows x 2 columns]
4238-
4239-
Args:
4240-
rule (str):
4241-
The offset string representing target conversion.
4242-
closed (Literal['right'] | Literal['left'] | None):
4243-
Which side of bin interval is closed. The default is 'left' for
4244-
all frequency offsets except for 'ME', 'YE', 'QE', 'BME', 'BA',
4245-
'BQE', and 'W' which all have a default of 'right'.
4246-
label (Literal['right'] | Literal['left'] | None):
4247-
Which bin edge label to label bucket with. The default is 'left'
4248-
for all frequency offsets except for 'ME', 'YE', 'QE', 'BME',
4249-
'BA', 'BQE', and 'W' which all have a default of 'right'.
4250-
on (str, default None):
4251-
For a DataFrame, column to use instead of index for resampling. Column
4252-
must be datetime-like.
4253-
level (str or int, default None):
4254-
For a MultiIndex, level (name or number) to use for resampling.
4255-
level must be datetime-like.
4256-
origin(str, default 'start_day'):
4257-
The timestamp on which to adjust the grouping. Must be one of the following:
4258-
'epoch': origin is 1970-01-01
4259-
'start': origin is the first value of the timeseries
4260-
'start_day': origin is the first day at midnight of the timeseries
4261-
Returns:
4262-
DataFrameGroupBy: DataFrameGroupBy object.
4263-
"""
42644200
block = self._block._generate_resample_label(
42654201
rule=rule,
42664202
closed=closed,

tests/system/small/test_dataframe.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5951,16 +5951,19 @@ def test_resample_with_column(
59515951
@pytest.mark.parametrize(
59525952
"rule",
59535953
[
5954-
# TODO(tswast): support timedeltas and dataoffsets
5954+
# TODO(tswast): support timedeltas and dataoffsets.
5955+
# TODO(tswast): support bins that default to "right".
59555956
"100d",
59565957
"1200h",
59575958
],
59585959
)
5959-
@pytest.mark.parametrize("closed", ["left", "right", None])
5960-
@pytest.mark.parametrize("label", ["left", "right", None])
5960+
# TODO(tswast): support "right"
5961+
@pytest.mark.parametrize("closed", ["left", None])
5962+
# TODO(tswast): support "right"
5963+
@pytest.mark.parametrize("label", ["left", None])
59615964
@pytest.mark.parametrize(
59625965
"origin",
5963-
["epoch", "start", "start_day", "end", "end_day"],
5966+
["epoch", "start", "start_day"], # TODO(tswast): support end, end_day.
59645967
)
59655968
def test_resample_with_index(
59665969
scalars_df_index,

tests/unit/test_dataframe.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,23 @@ def test_dataframe_repr_with_uninitialized_object():
4242
assert "DataFrame" in got
4343

4444

45-
@pytest.mark.parametrize("rule", [pd.DateOffset(weeks=1), pd.Timedelta(hours=8)])
45+
@pytest.mark.parametrize(
46+
"rule",
47+
[
48+
pd.DateOffset(weeks=1),
49+
pd.Timedelta(hours=8),
50+
# According to
51+
# https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.resample.html
52+
# these all default to "right" for closed and label, which isn't yet supported.
53+
"ME",
54+
"YE",
55+
"QE",
56+
"BME",
57+
"BA",
58+
"BQE",
59+
"W",
60+
],
61+
)
4662
def test_dataframe_rule_not_implememented(
4763
monkeypatch: pytest.MonkeyPatch,
4864
rule,
@@ -53,6 +69,41 @@ def test_dataframe_rule_not_implememented(
5369
dataframe.resample(rule=rule)
5470

5571

72+
def test_dataframe_closed_not_implememented(
73+
monkeypatch: pytest.MonkeyPatch,
74+
):
75+
dataframe = mocks.create_dataframe(monkeypatch)
76+
77+
with pytest.raises(NotImplementedError, match="Only closed='left'"):
78+
dataframe.resample(rule="1d", closed="right")
79+
80+
81+
def test_dataframe_label_not_implememented(
82+
monkeypatch: pytest.MonkeyPatch,
83+
):
84+
dataframe = mocks.create_dataframe(monkeypatch)
85+
86+
with pytest.raises(NotImplementedError, match="Only label='left'"):
87+
dataframe.resample(rule="1d", label="right")
88+
89+
90+
@pytest.mark.parametrize(
91+
"origin",
92+
[
93+
"end",
94+
"end_day",
95+
],
96+
)
97+
def test_dataframe_origin_not_implememented(
98+
monkeypatch: pytest.MonkeyPatch,
99+
origin,
100+
):
101+
dataframe = mocks.create_dataframe(monkeypatch)
102+
103+
with pytest.raises(NotImplementedError, match="origin"):
104+
dataframe.resample(rule="1d", origin=origin)
105+
106+
56107
def test_dataframe_setattr_with_uninitialized_object():
57108
"""Ensures DataFrame can be subclassed without trying to set attributes as columns."""
58109
# Avoid calling __init__ since it might be called later in a subclass.

third_party/bigframes_vendored/pandas/core/frame.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212
from __future__ import annotations
1313

14+
import datetime
1415
from typing import Hashable, Iterable, Literal, Optional, Sequence, Union
1516

1617
from bigframes_vendored import constants
@@ -4734,6 +4735,85 @@ def merge(
47344735
"""
47354736
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)
47364737

4738+
def resample(
4739+
self,
4740+
rule: str,
4741+
*,
4742+
closed: Optional[Literal["right", "left"]] = None,
4743+
label: Optional[Literal["right", "left"]] = None,
4744+
on=None,
4745+
level=None,
4746+
origin: Union[
4747+
Union[pd.Timestamp, datetime.datetime, np.datetime64, int, float, str],
4748+
Literal["epoch", "start", "start_day", "end", "end_day"],
4749+
] = "start_day",
4750+
):
4751+
"""Resample time-series data.
4752+
4753+
**Examples:**
4754+
4755+
>>> import bigframes.pandas as bpd
4756+
>>> data = {
4757+
... "timestamp_col": pd.date_range(
4758+
... start="2021-01-01 13:00:00", periods=30, freq="1s"
4759+
... ),
4760+
... "int64_col": range(30),
4761+
... "int64_too": range(10, 40),
4762+
... }
4763+
4764+
Resample on a DataFrame with index:
4765+
4766+
>>> df = bpd.DataFrame(data).set_index("timestamp_col")
4767+
>>> df.resample(rule="7s").min()
4768+
int64_col int64_too
4769+
2021-01-01 12:59:55 0 10
4770+
2021-01-01 13:00:02 2 12
4771+
2021-01-01 13:00:09 9 19
4772+
2021-01-01 13:00:16 16 26
4773+
2021-01-01 13:00:23 23 33
4774+
<BLANKLINE>
4775+
[5 rows x 2 columns]
4776+
4777+
Resample with column and origin set to 'start':
4778+
4779+
>>> df = bpd.DataFrame(data)
4780+
>>> df.resample(rule="7s", on = "timestamp_col", origin="start").min()
4781+
int64_col int64_too
4782+
2021-01-01 13:00:00 0 10
4783+
2021-01-01 13:00:07 7 17
4784+
2021-01-01 13:00:14 14 24
4785+
2021-01-01 13:00:21 21 31
4786+
2021-01-01 13:00:28 28 38
4787+
<BLANKLINE>
4788+
[5 rows x 2 columns]
4789+
4790+
Args:
4791+
rule (str):
4792+
The offset string representing target conversion.
4793+
Offsets 'ME', 'YE', 'QE', 'BME', 'BA', 'BQE', and 'W' are *not*
4794+
supported.
4795+
closed (Literal['left'] | None):
4796+
Which side of bin interval is closed. The default is 'left' for
4797+
all supported frequency offsets.
4798+
label (Literal['right'] | Literal['left'] | None):
4799+
Which bin edge label to label bucket with. The default is 'left'
4800+
for all supported frequency offsets.
4801+
on (str, default None):
4802+
For a DataFrame, column to use instead of index for resampling. Column
4803+
must be datetime-like.
4804+
level (str or int, default None):
4805+
For a MultiIndex, level (name or number) to use for resampling.
4806+
level must be datetime-like.
4807+
origin(str, default 'start_day'):
4808+
The timestamp on which to adjust the grouping. Must be one of the following:
4809+
'epoch': origin is 1970-01-01
4810+
'start': origin is the first value of the timeseries
4811+
'start_day': origin is the first day at midnight of the timeseries
4812+
Origin values 'end' and 'end_day' are *not* supported.
4813+
Returns:
4814+
DataFrameGroupBy: DataFrameGroupBy object.
4815+
"""
4816+
47374817
def round(self, decimals):
47384818
"""
47394819
Round a DataFrame to a variable number of decimal places.

0 commit comments

Comments
 (0)