Skip to content

Commit b86a7b9

Browse files
committed
Add note in whatsnew; address reviewer requests
1 parent d33922f commit b86a7b9

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

doc/source/whatsnew/v0.21.0.txt

+10
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ Other Enhancements
132132
Backwards incompatible API changes
133133
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134134

135+
.. _whatsnew_0210.api_breaking.Period:
136+
137+
pd.Period objects are now immutable
138+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139+
140+
:class:`Period` will now raise an ``AttributeError`` when a user tries to
141+
assign a new value to the `ordinal` or `freq` attributes. This will allow
142+
for reliable caching which will improve performance in indexing
143+
operations (:issue:`17116`).
144+
135145
.. _whatsnew_0210.api_breaking.pandas_eval:
136146

137147
Improved error handling during item assignment in pd.eval

pandas/_libs/period.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ from pandas import compat
1818
from pandas.compat import PY2
1919

2020
cimport cython
21-
# this is _libs.src.datetime, not python stdlib
2221
from datetime cimport *
2322

2423
cimport util, lib

pandas/tests/scalar/test_period.py

+10
Original file line numberDiff line numberDiff line change
@@ -1406,3 +1406,13 @@ def test_period_ops_offset(self):
14061406

14071407
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
14081408
p - offsets.Hour(2)
1409+
1410+
def test_period_immutable():
1411+
# see gh-17116
1412+
per = pd.Period('2014Q1')
1413+
with pytest.raises(AttributeError, message="is not writable"):
1414+
per.ordinal = 14
1415+
1416+
freq = per.freq
1417+
with pytest.raises(AttributeError, message="is not writable"):
1418+
per.freq = 2 * freq

pandas/tests/test_lib.py

-10
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,3 @@ def test_empty_like(self):
248248
expected = np.array([True])
249249

250250
self._check_behavior(arr, expected)
251-
252-
253-
def test_period_immutable():
254-
per = pd.Period('2014Q1')
255-
with pytest.raises(AttributeError, message="is not writable"):
256-
per.ordinal = 14
257-
258-
freq = per.freq
259-
with pytest.raises(AttributeError, message="is not writable"):
260-
per.freq = 2 * freq

0 commit comments

Comments
 (0)