8000 Make pd.Period immutable by jbrockmendel · Pull Request #17239 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Make pd.Period immutable #17239

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 15 commits into from
Aug 24, 2017
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
Prev Previous commit
Next Next commit
Add note in whatsnew; address reviewer requests
  • Loading branch information
jbrockmendel committed Aug 19, 2017
commit b86a7b9f455c154bafb674a210afd2b6f0c2915a
10 changes: 10 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ Other Enhancements
Backwards incompatible API changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. _whatsnew_0210.api_breaking.Period:

pd.Period objects are now immutable
Copy link
Contributor

Choose a reason for hiding this comment

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

just say Period

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:class:`Period` will now raise an ``AttributeError`` when a user tries to
assign a new value to the `ordinal` or `freq` attributes. This will allow
Copy link
Contributor

Choose a reason for hiding this comment

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

you dont need to provide an explanation.

for reliable caching which will improve performance in indexing
operations (:issue:`17116`).

.. _whatsnew_0210.api_breaking.pandas_eval:

Improved error handling during item assignment in pd.eval
Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ from pandas import compat
from pandas.compat import PY2

cimport cython
# this is _libs.src.datetime, not python stdlib
from datetime cimport *

cimport util, lib
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/scalar/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,3 +1406,13 @@ def test_period_ops_offset(self):

with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
p - offsets.Hour(2)

def test_period_immutable():
# see gh-17116
per = pd.Period('2014Q1')
with pytest.raises(AttributeError, message="is not writable"):
Copy link
Member

Choose a reason for hiding this comment

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

the message="is not writable" is not needed here, the default assertion message of pytest is normally clear.

If you actually want to check the message of the AttributeError, it is the match keyword you need to use, not message (but I don't think it is needed to test that, it is not a custom error message we write ourselves)

Copy link
Member Author

Choose a reason for hiding this comment

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

Pushing change now.

per.ordinal = 14

freq = per.freq
with pytest.raises(AttributeError, message="is not writable"):
per.freq = 2 * freq
10 changes: 0 additions & 10 deletions pandas/tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,3 @@ def test_empty_like(self):
expected = np.array([True])

self._check_behavior(arr, expected)


def test_period_immutable():
per = pd.Period('2014Q1')
with pytest.raises(AttributeError, message="is not writable"):
per.ordinal = 14

freq = per.freq
with pytest.raises(AttributeError, message="is not writable"):
per.freq = 2 * freq
0