Skip to content

Commit 41ec93a

Browse files
authored
REGR: Make DateOffset immutable (#36946)
1 parent 9e6b50f commit 41ec93a

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

doc/source/whatsnew/v1.1.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414

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

1919
.. ---------------------------------------------------------------------------
2020

pandas/_libs/tslibs/offsets.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1212,9 +1212,8 @@ class DateOffset(RelativeDeltaOffset, metaclass=OffsetMeta):
12121212
>>> ts + DateOffset(months=2)
12131213
Timestamp('2017-03-01 09:10:11')
12141214
"""
1215-
1216-
pass
1217-
1215+
def __setattr__(self, name, value):
1216+
raise AttributeError("DateOffset objects are immutable.")
12181217

12191218
# --------------------------------------------------------------------
12201219

pandas/tests/tseries/offsets/test_offsets.py

+17
Original file line numberDiff line numberDiff line change
@@ -4424,3 +4424,20 @@ def test_week_add_invalid():
44244424
other = Day()
44254425
with pytest.raises(TypeError, match="Cannot add"):
44264426
offset + other
4427+
4428+
4429+
@pytest.mark.parametrize(
4430+
"attribute",
4431+
[
4432+
"hours",
4433+
"days",
4434+
"weeks",
4435+
"months",
4436+
"years",
4437+
],
4438+
)
4439+
def test_dateoffset_immutable(attribute):
4440+
offset = DateOffset(**{attribute: 0})
4441+
msg = "DateOffset objects are immutable"
4442+
with pytest.raises(AttributeError, match=msg):
4443+
setattr(offset, attribute, 5)

0 commit comments

Comments
 (0)