Skip to content
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

Make pd.Period immutable #17239

Merged
merged 15 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 13 additions & 8 deletions pandas/_libs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import operator

from cpython cimport (
PyObject_RichCompareBool,
Py_EQ, Py_NE,
)
Py_EQ, Py_NE)

from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray,
NPY_INT64, NPY_DATETIME, NPY_TIMEDELTA)
Expand All @@ -19,8 +18,11 @@ from pandas import compat
from pandas.compat import PY2

cimport cython
# this is _libs.src.datetime, not python stdlib
Copy link
Contributor

Choose a reason for hiding this comment

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

don't add superfluous comments

Copy link
Member Author

Choose a reason for hiding this comment

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

Similar comments exist elsewhere e.g. in lib.pyx:

# this is our tseries.pxd
from datetime cimport *

This name overlap is an easy source of confusion.

Not worth arguing about so I'll change it. Just sayin'.

from datetime cimport *

cimport util, lib

from lib cimport is_null_datetimelike, is_period
from pandas._libs import tslib, lib
from pandas._libs.tslib import (Timedelta, Timestamp, iNaT,
Expand Down Expand Up @@ -683,13 +685,17 @@ class IncompatibleFrequency(ValueError):

cdef class _Period(object):

cdef public:
cdef readonly:
int64_t ordinal
object freq

_comparables = ['name', 'freqstr']
_typ = 'period'

def __cinit__(self, ordinal, freq):
self.ordinal = ordinal
self.freq = freq

@classmethod
def _maybe_convert_freq(cls, object freq):

Expand All @@ -713,9 +719,8 @@ cdef class _Period(object):
if ordinal == iNaT:
return NaT
else:
self = _Period.__new__(cls)
self.ordinal = ordinal
self.freq = cls._maybe_convert_freq(freq)
freq = cls._maybe_convert_freq(freq)
self = _Period.__new__(cls, ordinal, freq)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be self = cls.__new__...?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so. cls._from_ordinal gets called at the end of Period.__new__, so I expect it explicitly needs to be _Period.

return self

def __richcmp__(self, other, op):
Expand Down Expand Up @@ -767,7 +772,7 @@ cdef class _Period(object):
def __add__(self, other):
if isinstance(self, Period):
if isinstance(other, (timedelta, np.timedelta64,
offsets.Tick, offsets.DateOffset,
offsets.DateOffset,
Timedelta)):
return self._add_delta(other)
elif other is NaT:
Expand All @@ -785,7 +790,7 @@ cdef class _Period(object):
def __sub__(self, other):
if isinstance(self, Period):
if isinstance(other, (timedelta, np.timedelta64,
offsets.Tick, offsets.DateOffset,
offsets.DateOffset,
Timedelta)):
neg_other = -other
return self + neg_other
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,13 @@ def test_empty_like(self):
expected = np.array([True])

self._check_behavior(arr, expected)


def test_period_immutable():
per = pd.Period('2014Q1')
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number here. move test to tests for period scalars.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

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