Skip to content

Commit cf4974d

Browse files
Backport PR pandas-dev#36946: REGR: Make DateOffset immutable (pandas-dev#36992)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent dbc9662 commit cf4974d

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
@@ -1209,9 +1209,8 @@ class DateOffset(RelativeDeltaOffset, metaclass=OffsetMeta):
12091209
>>> ts + DateOffset(months=2)
12101210
Timestamp('2017-03-01 09:10:11')
12111211
"""
1212-
1213-
pass
1214-
1212+
def __setattr__(self, name, value):
1213+
raise AttributeError("DateOffset objects are immutable.")
12151214

12161215
# --------------------------------------------------------------------
12171216

pandas/tests/tseries/offsets/test_offsets.py

+17
Original file line numberDiff line numberDiff line change
@@ -4438,3 +4438,20 @@ def test_week_add_invalid():
44384438
other = Day()
44394439
with pytest.raises(TypeError, match="Cannot add"):
44404440
offset + other
4441+
4442+
4443+
@pytest.mark.parametrize(
4444+
"attribute",
4445+
[
4446+
"hours",
4447+
"days",
4448+
"weeks",
4449+
"months",
4450+
"years",
4451+
],
4452+
)
4453+
def test_dateoffset_immutable(attribute):
4454+
offset = DateOffset(**{attribute: 0})
4455+
msg = "DateOffset objects are immutable"
4456+
with pytest.raises(AttributeError, match=msg):
4457+
setattr(offset, attribute, 5)

0 commit comments

Comments
 (0)