Skip to content

Commit d6df8ea

Browse files
jbrockmendeljreback
authored andcommitted
Follow up to #17422 (#17472)
1 parent 3ccb88c commit d6df8ea

File tree

4 files changed

+38
-152
lines changed

4 files changed

+38
-152
lines changed

pandas/_libs/period.pyx

+27-28
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ from cpython cimport (
1010
from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray,
1111
NPY_INT64, NPY_DATETIME, NPY_TIMEDELTA)
1212
import numpy as np
13+
import_array()
1314

1415
from libc.stdlib cimport free
1516

16-
from pandas import compat
1717
from pandas.compat import PY2
1818

1919
cimport cython
2020

2121
from datetime cimport (
2222
is_leapyear,
23-
PyDateTime_IMPORT,
2423
pandas_datetimestruct,
2524
pandas_datetimestruct_to_datetime,
2625
pandas_datetime_to_datetimestruct,
@@ -29,6 +28,7 @@ from datetime cimport (
2928

3029

3130
cimport util, lib
31+
from util cimport is_period_object, is_string_object
3232

3333
from lib cimport is_null_datetimelike, is_period
3434
from pandas._libs import tslib, lib
@@ -41,6 +41,8 @@ from tslib cimport (
4141
_get_dst_info,
4242
_nat_scalar_rules)
4343

44+
from tslibs.frequencies cimport get_freq_code
45+
4446
from pandas.tseries import offsets
4547
from pandas.core.tools.datetimes import parse_time_string
4648
from pandas.tseries import frequencies
@@ -329,8 +331,6 @@ cdef list str_extra_fmts = ["^`AB`^", "^`CD`^", "^`EF`^",
329331
"^`GH`^", "^`IJ`^", "^`KL`^"]
330332

331333
cdef object _period_strftime(int64_t value, int freq, object fmt):
332-
import sys
333-
334334
cdef:
335335
Py_ssize_t i
336336
date_info dinfo
@@ -683,7 +683,7 @@ cdef class _Period(object):
683683
def _maybe_convert_freq(cls, object freq):
684684

685685
if isinstance(freq, (int, tuple)):
686-
code, stride = frequencies.get_freq_code(freq)
686+
code, stride = get_freq_code(freq)
687687
freq = frequencies._get_freq_str(code, stride)
688688

689689
freq = frequencies.to_offset(freq)
@@ -707,7 +707,7 @@ cdef class _Period(object):
707707
return self
708708

709709
def __richcmp__(self, other, op):
710-
if isinstance(other, Period):
710+
if is_period_object(other):
711711
if other.freq != self.freq:
712712
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
713713
raise IncompatibleFrequency(msg)
@@ -753,7 +753,7 @@ cdef class _Period(object):
753753
return NotImplemented
754754

755755
def __add__(self, other):
756-
if isinstance(self, Period):
756+
if is_period_object(self):
757757
if isinstance(other, (timedelta, np.timedelta64,
758758
offsets.DateOffset,
759759
Timedelta)):
@@ -765,13 +765,13 @@ cdef class _Period(object):
765765
return Period(ordinal=ordinal, freq=self.freq)
766766
else: # pragma: no cover
767767
return NotImplemented
768-
elif isinstance(other, Period):
768+
elif is_period_object(other):
769769
return other + self
770770
else:
771771
return NotImplemented
772772

773773
def __sub__(self, other):
774-
if isinstance(self, Period):
774+
if is_period_object(self):
775775
if isinstance(other, (timedelta, np.timedelta64,
776776
offsets.DateOffset,
777777
Timedelta)):
@@ -780,7 +780,7 @@ cdef class _Period(object):
780780
elif lib.is_integer(other):
781781
ordinal = self.ordinal - other * self.freq.n
782782
return Period(ordinal=ordinal, freq=self.freq)
783-
elif isinstance(other, Period):
783+
elif is_period_object(other):
784784
if other.freq != self.freq:
785785
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
786786
raise IncompatibleFrequency(msg)
@@ -789,7 +789,7 @@ cdef class _Period(object):
789789
return -other.__sub__(self)
790790
else: # pragma: no cover
791791
return NotImplemented
792-
elif isinstance(other, Period):
792+
elif is_period_object(other):
793793
if self is NaT:
794794
return NaT
795795
return NotImplemented
@@ -813,8 +813,8 @@ cdef class _Period(object):
813813
"""
814814
freq = self._maybe_convert_freq(freq)
815815
how = _validate_end_alias(how)
816-
base1, mult1 = frequencies.get_freq_code(self.freq)
817-
base2, mult2 = frequencies.get_freq_code(freq)
816+
base1, mult1 = get_freq_code(self.freq)
817+
base2, mult2 = get_freq_code(freq)
818818

819819
# mult1 can't be negative or 0
820820
end = how == 'E'
@@ -860,17 +860,17 @@ cdef class _Period(object):
860860
how = _validate_end_alias(how)
861861

862862
if freq is None:
863-
base, mult = frequencies.get_freq_code(self.freq)
863+
base, mult = get_freq_code(self.freq)
864864
freq = frequencies.get_to_timestamp_base(base)
865865

866-
base, mult = frequencies.get_freq_code(freq)
866+
base, mult = get_freq_code(freq)
867867
val = self.asfreq(freq, how)
868868

869869
dt64 = period_ordinal_to_dt64(val.ordinal, base)
870870
return Timestamp(dt64, tz=tz)
871871

872872
cdef _field(self, alias):
873-
base, mult = frequencies.get_freq_code(self.freq)
873+
base, mult = get_freq_code(self.freq)
874874
return get_period_field(alias, self.ordinal, base)
875875

876876
property year:
@@ -935,7 +935,7 @@ cdef class _Period(object):
935935
return self.freq.freqstr
936936

937937
def __repr__(self):
938-
base, mult = frequencies.get_freq_code(self.freq)
938+
base, mult = get_freq_code(self.freq)
939939
formatted = period_format(self.ordinal, base)
940940
return "Period('%s', '%s')" % (formatted, self.freqstr)
941941

@@ -946,7 +946,7 @@ cdef class _Period(object):
946946
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
947947
py2/py3.
948948
"""
949-
base, mult = frequencies.get_freq_code(self.freq)
949+
base, mult = get_freq_code(self.freq)
950950
formatted = period_format(self.ordinal, base)
951951
value = ("%s" % formatted)
952952
return value
@@ -1096,7 +1096,7 @@ cdef class _Period(object):
10961096
>>> a.strftime('%b. %d, %Y was a %A')
10971097
'Jan. 01, 2001 was a Monday'
10981098
"""
1099-
base, mult = frequencies.get_freq_code(self.freq)
1099+
base, mult = get_freq_code(self.freq)
11001100
return period_format(self.ordinal, base, fmt)
11011101

11021102

@@ -1161,10 +1161,10 @@ class Period(_Period):
11611161
ordinal = _ordinal_from_fields(year, month, quarter, day,
11621162
hour, minute, second, freq)
11631163

1164-
elif isinstance(value, Period):
1164+
elif is_period_object(value):
11651165
other = value
1166-
if freq is None or frequencies.get_freq_code(
1167-
freq) == frequencies.get_freq_code(other.freq):
1166+
if freq is None or get_freq_code(
1167+
freq) == get_freq_code(other.freq):
11681168
ordinal = other.ordinal
11691169
freq = other.freq
11701170
else:
@@ -1174,7 +1174,7 @@ class Period(_Period):
11741174
elif is_null_datetimelike(value) or value in tslib._nat_strings:
11751175
ordinal = iNaT
11761176

1177-
elif isinstance(value, compat.string_types) or lib.is_integer(value):
1177+
elif is_string_object(value) or lib.is_integer(value):
11781178
if lib.is_integer(value):
11791179
value = str(value)
11801180
value = value.upper()
@@ -1191,7 +1191,7 @@ class Period(_Period):
11911191
dt = value
11921192
if freq is None:
11931193
raise ValueError('Must supply freq for datetime value')
1194-
elif isinstance(value, np.datetime64):
1194+
elif util.is_datetime64_object(value):
11951195
dt = Timestamp(value)
11961196
if freq is None:
11971197
raise ValueError('Must supply freq for datetime value')
@@ -1204,7 +1204,7 @@ class Period(_Period):
12041204
raise ValueError(msg)
12051205

12061206
if ordinal is None:
1207-
base, mult = frequencies.get_freq_code(freq)
1207+
base, mult = get_freq_code(freq)
12081208
ordinal = get_period_ordinal(dt.year, dt.month, dt.day,
12091209
dt.hour, dt.minute, dt.second,
12101210
dt.microsecond, 0, base)
@@ -1214,7 +1214,7 @@ class Period(_Period):
12141214

12151215
def _ordinal_from_fields(year, month, quarter, day,
12161216
hour, minute, second, freq):
1217-
base, mult = frequencies.get_freq_code(freq)
1217+
base, mult = get_freq_code(freq)
12181218
if quarter is not None:
12191219
year, month = _quarter_to_myear(year, quarter, freq)
12201220

@@ -1227,8 +1227,7 @@ def _quarter_to_myear(year, quarter, freq):
12271227
if quarter <= 0 or quarter > 4:
12281228
raise ValueError('Quarter must be 1 <= q <= 4')
12291229

1230-
mnum = frequencies._month_numbers[
1231-
frequencies._get_rule_month(freq)] + 1
1230+
mnum = tslib._MONTH_NUMBERS[tslib._get_rule_month(freq)] + 1
12321231
month = (mnum + (quarter - 1) * 3) % 12 + 1
12331232
if month > mnum:
12341233
year -= 1

pandas/_libs/tslibs/frequencies.pxd

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# -*- coding: utf-8 -*-
2+
# cython: profile=False
3+
4+
cpdef get_freq_code(freqstr)

pandas/_libs/tslibs/frequencies.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ _period_code_map = {
150150
"N": 12000, # Nanosecondly
151151
}
152152

153+
_reverse_period_code_map = {
154+
_period_code_map[key]: key for key in _period_code_map}
155+
153156
# Yearly aliases; careful not to put these in _reverse_period_code_map
154157
_period_code_map.update({'Y' + key[1:]: _period_code_map[key]
155158
for key in _period_code_map

0 commit comments

Comments
 (0)