Skip to content

CLN: disallow tuple in to_offset #34703

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

Merged
merged 7 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 5 additions & 8 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3583,10 +3583,10 @@ cpdef to_offset(freq):
>>> to_offset("1D1H")
<25 * Hours>

>>> to_offset(("W", 2))
>>> to_offset("2W")
<2 * Weeks: weekday=6>

>>> to_offset((2, "B"))
>>> to_offset("2B")
<2 * BusinessDays>

>>> to_offset(pd.Timedelta(days=1))
Expand All @@ -3602,12 +3602,9 @@ cpdef to_offset(freq):
return freq

if isinstance(freq, tuple):
name = freq[0]
stride = freq[1]
if isinstance(stride, str):
name, stride = stride, name
name, _ = base_and_stride(name)
delta = _get_offset(name) * stride
raise TypeError(
f"to_offset does not support tuples {freq}, pass as a string instead"
)

elif isinstance(freq, timedelta):
return delta_to_tick(freq)
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,8 @@ def test_datetimeindex_constructor_misc(self):
assert idx[0] == sdate + 0 * offsets.BDay()
assert idx.freq == "B"

idx = date_range(end=edate, freq=("D", 5), periods=20)
assert len(idx) == 20
assert idx[-1] == edate
assert idx.freq == "5D"
with pytest.raises(TypeError, match="pass as a string instead"):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this with the other error tests?

date_range(end=edate, freq=("D", 5), periods=20)

idx1 = date_range(start=sdate, end=edate, freq="W-SUN")
idx2 = date_range(start=sdate, end=edate, freq=offsets.Week(weekday=6))
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/indexes/period/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,8 @@ def test_constructor(self):
assert (i1 == i2).all()
assert i1.freq == i2.freq

end_intv = Period("2006-12-31", ("w", 1))
i2 = period_range(end=end_intv, periods=10)
assert len(i1) == len(i2)
assert (i1 == i2).all()
assert i1.freq == i2.freq
with pytest.raises(TypeError, match="pass as a string instead"):
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Period("2006-12-31", ("w", 1))

end_intv = Period("2005-05-01", "B")
i1 = period_range(start=start, end=end_intv)
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ def test_period_index_length(self):
assert (i1 == i2).all()
assert i1.freq == i2.freq

end_intv = Period("2006-12-31", ("w", 1))
i2 = period_range(end=end_intv, periods=10)
assert len(i1) == len(i2)
assert (i1 == i2).all()
assert i1.freq == i2.freq

msg = "start and end must have same freq"
with pytest.raises(ValueError, match=msg):
period_range(start=start, end=end_intv)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def test_construction(self):
i1 = Period("1982", freq="min")
i2 = Period("1982", freq="MIN")
assert i1 == i2
i2 = Period("1982", freq=("Min", 1))
assert i1 == i2

with pytest.raises(TypeError, match="pass as a string instead"):
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Period("1982", freq=("Min", 1))

i1 = Period(year=2005, month=3, day=1, freq="D")
i2 = Period("3/1/2005", freq="D")
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/tslibs/test_to_offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[
(to_offset("10us"), offsets.Micro(10)),
(offsets.Hour(), offsets.Hour()),
((5, "T"), offsets.Minute(5)),
("2h30min", offsets.Minute(150)),
("2h 30min", offsets.Minute(150)),
("2h30min15s", offsets.Second(150 * 60 + 15)),
Expand Down Expand Up @@ -89,10 +88,16 @@ def test_to_offset_invalid(freqstr):


def test_to_offset_no_evaluate():
with pytest.raises(ValueError, match="Could not evaluate"):
msg = str(("", ""))
with pytest.raises(TypeError, match=msg):
to_offset(("", ""))


def test_to_offset_tuple_unsupported():
with pytest.raises(TypeError, match="pass as a string instead"):
to_offset((5, "T"))


@pytest.mark.parametrize(
"freqstr,expected",
[
Expand Down