Skip to content

REGR: Make DateOffset immutable #36946

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 1 commit into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression where attempting to mutate a :class:`DateOffset` object would no longer raise an ``AttributeError`` (:issue:`36940`)

.. ---------------------------------------------------------------------------

Expand Down
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,8 @@ class DateOffset(RelativeDeltaOffset, metaclass=OffsetMeta):
>>> ts + DateOffset(months=2)
Timestamp('2017-03-01 09:10:11')
"""

pass

def __setattr__(self, name, value):
raise AttributeError("DateOffset objects are immutable.")

# --------------------------------------------------------------------

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4424,3 +4424,20 @@ def test_week_add_invalid():
other = Day()
with pytest.raises(TypeError, match="Cannot add"):
offset + other


@pytest.mark.parametrize(
"attribute",
[
"hours",
"days",
"weeks",
"months",
"years",
],
)
def test_dateoffset_immutable(attribute):
offset = DateOffset(**{attribute: 0})
msg = "DateOffset objects are immutable"
with pytest.raises(AttributeError, match=msg):
setattr(offset, attribute, 5)