Skip to content

Commit 89cf9d6

Browse files
committed
Make Period ordinal and freq readonly
Remove redundant isinstance(freq, (Tick, DateOffset)) check, since Tick inherits from DateOffset flake8 cleanup of cpython imports
1 parent 06850a1 commit 89cf9d6

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pandas/_libs/period.pyx

+13-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import operator
33

44
from cpython cimport (
55
PyObject_RichCompareBool,
6-
Py_EQ, Py_NE,
7-
)
6+
Py_EQ, Py_NE)
87

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

2120
cimport cython
21+
# this is _libs.src.datetime, not python stdlib
2222
from datetime cimport *
23+
2324
cimport util, lib
25+
2426
from lib cimport is_null_datetimelike, is_period
2527
from pandas._libs import tslib, lib
2628
from pandas._libs.tslib import (Timedelta, Timestamp, iNaT,
@@ -683,13 +685,17 @@ class IncompatibleFrequency(ValueError):
683685

684686
cdef class _Period(object):
685687

686-
cdef public:
688+
cdef readonly:
687689
int64_t ordinal
688690
object freq
689691

690692
_comparables = ['name', 'freqstr']
691693
_typ = 'period'
692694

695+
def __cinit__(self, ordinal, freq):
696+
self.ordinal = ordinal
697+
self.freq = freq
698+
693699
@classmethod
694700
def _maybe_convert_freq(cls, object freq):
695701

@@ -713,9 +719,8 @@ cdef class _Period(object):
713719
if ordinal == iNaT:
714720
return NaT
715721
else:
716-
self = _Period.__new__(cls)
717-
self.ordinal = ordinal
718-
self.freq = cls._maybe_convert_freq(freq)
722+
freq = cls._maybe_convert_freq(freq)
723+
self = _Period.__new__(cls, ordinal, freq)
719724
return self
720725

721726
def __richcmp__(self, other, op):
@@ -767,7 +772,7 @@ cdef class _Period(object):
767772
def __add__(self, other):
768773
if isinstance(self, Period):
769774
if isinstance(other, (timedelta, np.timedelta64,
770-
offsets.Tick, offsets.DateOffset,
775+
offsets.DateOffset,
771776
Timedelta)):
772777
return self._add_delta(other)
773778
elif other is NaT:
@@ -785,7 +790,7 @@ cdef class _Period(object):
785790
def __sub__(self, other):
786791
if isinstance(self, Period):
787792
if isinstance(other, (timedelta, np.timedelta64,
788-
offsets.Tick, offsets.DateOffset,
793+
offsets.DateOffset,
789794
Timedelta)):
790795
neg_other = -other
791796
return self + neg_other

0 commit comments

Comments
 (0)