From f71bf1721b5ce9569543a2f408ed51b55c25bb4b Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 24 Mar 2020 12:39:31 +0200 Subject: [PATCH 1/5] PERF: Using _Period for optimization --- pandas/_libs/index.pyx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 0ba5cb7e9bc40..997727436374a 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -12,7 +12,6 @@ cnp.import_array() cimport pandas._libs.util as util -from pandas._libs.tslibs import Period from pandas._libs.tslibs.nattype cimport c_NaT as NaT from pandas._libs.tslibs.c_timestamp cimport _Timestamp @@ -468,9 +467,8 @@ cdef class PeriodEngine(Int64Engine): cdef int64_t _unbox_scalar(self, scalar) except? -1: if scalar is NaT: return scalar.value - if isinstance(scalar, Period): + if isinstance(scalar, periodlib._Period): # NB: we assume that we have the correct freq here. - # TODO: potential optimize by checking for _Period? return scalar.ordinal raise TypeError(scalar) From 1fdbf1b2864cdbeab78a1b49a54e860b7742c7b4 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 24 Mar 2020 19:31:12 +0200 Subject: [PATCH 2/5] cimporting _Period --- pandas/_libs/index.pyx | 3 ++- pandas/_libs/tslibs/period.pxd | 8 ++++++++ pandas/_libs/tslibs/period.pyx | 6 +++--- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 pandas/_libs/tslibs/period.pxd diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 997727436374a..fcf87d3552175 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -19,6 +19,7 @@ from pandas._libs.hashtable cimport HashTable from pandas._libs import algos, hashtable as _hash from pandas._libs.tslibs import Timedelta, period as periodlib +from pandas._libs.tslibs.period cimport c_Period from pandas._libs.missing import checknull @@ -467,7 +468,7 @@ cdef class PeriodEngine(Int64Engine): cdef int64_t _unbox_scalar(self, scalar) except? -1: if scalar is NaT: return scalar.value - if isinstance(scalar, periodlib._Period): + if isinstance(scalar, c_Period): # NB: we assume that we have the correct freq here. return scalar.ordinal raise TypeError(scalar) diff --git a/pandas/_libs/tslibs/period.pxd b/pandas/_libs/tslibs/period.pxd new file mode 100644 index 0000000000000..029b316c9a566 --- /dev/null +++ b/pandas/_libs/tslibs/period.pxd @@ -0,0 +1,8 @@ +from numpy cimport int64_t + +cdef class _Period: + cdef readonly: + int64_t ordinal + object freq + +cdef _Period c_Period diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index c3a47902cff0f..2fc74812d0785 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1555,9 +1555,9 @@ class IncompatibleFrequency(ValueError): cdef class _Period: - cdef readonly: - int64_t ordinal - object freq + #cdef readonly: + #int64_t ordinal + #object freq _typ = 'period' From 5f7a6ebc81a3211bf7039a9e5e29366f158a6046 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 24 Mar 2020 20:13:30 +0200 Subject: [PATCH 3/5] Minor fix --- pandas/_libs/index.pyx | 4 ++-- pandas/_libs/tslibs/period.pxd | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index fcf87d3552175..8b3ac94dabdca 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -19,7 +19,7 @@ from pandas._libs.hashtable cimport HashTable from pandas._libs import algos, hashtable as _hash from pandas._libs.tslibs import Timedelta, period as periodlib -from pandas._libs.tslibs.period cimport c_Period +from pandas._libs.tslibs.period cimport _Period from pandas._libs.missing import checknull @@ -468,7 +468,7 @@ cdef class PeriodEngine(Int64Engine): cdef int64_t _unbox_scalar(self, scalar) except? -1: if scalar is NaT: return scalar.value - if isinstance(scalar, c_Period): + if isinstance(scalar, _Period): # NB: we assume that we have the correct freq here. return scalar.ordinal raise TypeError(scalar) diff --git a/pandas/_libs/tslibs/period.pxd b/pandas/_libs/tslibs/period.pxd index 029b316c9a566..fdb0222e269f5 100644 --- a/pandas/_libs/tslibs/period.pxd +++ b/pandas/_libs/tslibs/period.pxd @@ -4,5 +4,3 @@ cdef class _Period: cdef readonly: int64_t ordinal object freq - -cdef _Period c_Period From 19085037e1680d8a6198ffc61f350ae41e898be5 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 25 Mar 2020 09:45:03 +0200 Subject: [PATCH 4/5] Lint issues --- pandas/_libs/tslibs/period.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 2fc74812d0785..783e510d759f7 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1555,9 +1555,9 @@ class IncompatibleFrequency(ValueError): cdef class _Period: - #cdef readonly: - #int64_t ordinal - #object freq + # cdef readonly: + # int64_t ordinal + # object freq _typ = 'period' From 6ec6c664e43463f41172bbadd14285bc9001ea31 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 29 Mar 2020 19:33:39 +0300 Subject: [PATCH 5/5] Revert changes --- pandas/_libs/index.pyx | 4 ++-- pandas/_libs/tslibs/period.pxd | 6 ------ pandas/_libs/tslibs/period.pyx | 6 +++--- 3 files changed, 5 insertions(+), 11 deletions(-) delete mode 100644 pandas/_libs/tslibs/period.pxd diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index e395d5e19a3e4..4a9b504ffb0d9 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -21,6 +21,7 @@ cnp.import_array() cimport pandas._libs.util as util +from pandas._libs.tslibs import Period from pandas._libs.tslibs.nattype cimport c_NaT as NaT from pandas._libs.tslibs.c_timestamp cimport _Timestamp @@ -28,7 +29,6 @@ from pandas._libs.hashtable cimport HashTable from pandas._libs import algos, hashtable as _hash from pandas._libs.tslibs import Timedelta, period as periodlib -from pandas._libs.tslibs.period cimport _Period from pandas._libs.missing import checknull @@ -477,7 +477,7 @@ cdef class PeriodEngine(Int64Engine): cdef int64_t _unbox_scalar(self, scalar) except? -1: if scalar is NaT: return scalar.value - if isinstance(scalar, _Period): + if isinstance(scalar, Period): # NB: we assume that we have the correct freq here. return scalar.ordinal raise TypeError(scalar) diff --git a/pandas/_libs/tslibs/period.pxd b/pandas/_libs/tslibs/period.pxd deleted file mode 100644 index fdb0222e269f5..0000000000000 --- a/pandas/_libs/tslibs/period.pxd +++ /dev/null @@ -1,6 +0,0 @@ -from numpy cimport int64_t - -cdef class _Period: - cdef readonly: - int64_t ordinal - object freq diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 783e510d759f7..c3a47902cff0f 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1555,9 +1555,9 @@ class IncompatibleFrequency(ValueError): cdef class _Period: - # cdef readonly: - # int64_t ordinal - # object freq + cdef readonly: + int64_t ordinal + object freq _typ = 'period'