Skip to content

Commit 1eccd56

Browse files
committed
Follow up to pandas-dev#17422
Implement .pxd file for tslibs.frequencies cimport get_freq_code in period.pyx Replace isinstance checks in period.pyx with C-equivalents form util Remove unused imports from period.pyx Remove code in tseries.frequencies that can now be imported from tslibs.frequencies
1 parent 7e4e8ac commit 1eccd56

File tree

4 files changed

+37
-152
lines changed

4 files changed

+37
-152
lines changed

pandas/_libs/period.pyx

+26-28
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import numpy as np
1313

1414
from libc.stdlib cimport free
1515

16-
from pandas import compat
1716
from pandas.compat import PY2
1817

1918
cimport cython
2019

2120
from datetime cimport (
2221
is_leapyear,
23-
PyDateTime_IMPORT,
2422
pandas_datetimestruct,
2523
pandas_datetimestruct_to_datetime,
2624
pandas_datetime_to_datetimestruct,
@@ -29,6 +27,7 @@ from datetime cimport (
2927

3028

3129
cimport util, lib
30+
from util cimport is_period_object, is_string_object
3231

3332
from lib cimport is_null_datetimelike, is_period
3433
from pandas._libs import tslib, lib
@@ -41,6 +40,8 @@ from tslib cimport (
4140
_get_dst_info,
4241
_nat_scalar_rules)
4342

43+
from tslibs.frequencies cimport get_freq_code
44+
4445
from pandas.tseries import offsets
4546
from pandas.core.tools.datetimes import parse_time_string
4647
from pandas.tseries import frequencies
@@ -329,8 +330,6 @@ cdef list str_extra_fmts = ["^`AB`^", "^`CD`^", "^`EF`^",
329330
"^`GH`^", "^`IJ`^", "^`KL`^"]
330331

331332
cdef object _period_strftime(int64_t value, int freq, object fmt):
332-
import sys
333-
334333
cdef:
335334
Py_ssize_t i
336335
date_info dinfo
@@ -683,7 +682,7 @@ cdef class _Period(object):
683682
def _maybe_convert_freq(cls, object freq):
684683

685684
if isinstance(freq, (int, tuple)):
686-
code, stride = frequencies.get_freq_code(freq)
685+
code, stride = get_freq_code(freq)
687686
freq = frequencies._get_freq_str(code, stride)
688687

689688
freq = frequencies.to_offset(freq)
@@ -707,7 +706,7 @@ cdef class _Period(object):
707706
return self
708707

709708
def __richcmp__(self, other, op):
710-
if isinstance(other, Period):
709+
if is_period_object(other):
711710
if other.freq != self.freq:
712711
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
713712
raise IncompatibleFrequency(msg)
@@ -753,7 +752,7 @@ cdef class _Period(object):
753752
return NotImplemented
754753

755754
def __add__(self, other):
756-
if isinstance(self, Period):
755+
if is_period_object(self):
757756
if isinstance(other, (timedelta, np.timedelta64,
758757
offsets.DateOffset,
759758
Timedelta)):
@@ -765,13 +764,13 @@ cdef class _Period(object):
765764
return Period(ordinal=ordinal, freq=self.freq)
766765
else: # pragma: no cover
767766
return NotImplemented
768-
elif isinstance(other, Period):
767+
elif is_period_object(other):
769768
return other + self
770769
else:
771770
return NotImplemented
772771

773772
def __sub__(self, other):
774-
if isinstance(self, Period):
773+
if is_period_object(self):
775774
if isinstance(other, (timedelta, np.timedelta64,
776775
offsets.DateOffset,
777776
Timedelta)):
@@ -780,7 +779,7 @@ cdef class _Period(object):
780779
elif lib.is_integer(other):
781780
ordinal = self.ordinal - other * self.freq.n
782781
return Period(ordinal=ordinal, freq=self.freq)
783-
elif isinstance(other, Period):
782+
elif is_period_object(other):
784783
if other.freq != self.freq:
785784
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
786785
raise IncompatibleFrequency(msg)
@@ -789,7 +788,7 @@ cdef class _Period(object):
789788
return -other.__sub__(self)
790789
else: # pragma: no cover
791790
return NotImplemented
792-
elif isinstance(other, Period):
791+
elif is_period_object(other):
793792
if self is NaT:
794793
return NaT
795794
return NotImplemented
@@ -813,8 +812,8 @@ cdef class _Period(object):
813812
"""
814813
freq = self._maybe_convert_freq(freq)
815814
how = _validate_end_alias(how)
816-
base1, mult1 = frequencies.get_freq_code(self.freq)
817-
base2, mult2 = frequencies.get_freq_code(freq)
815+
base1, mult1 = get_freq_code(self.freq)
816+
base2, mult2 = get_freq_code(freq)
818817

819818
# mult1 can't be negative or 0
820819
end = how == 'E'
@@ -860,17 +859,17 @@ cdef class _Period(object):
860859
how = _validate_end_alias(how)
861860

862861
if freq is None:
863-
base, mult = frequencies.get_freq_code(self.freq)
862+
base, mult = get_freq_code(self.freq)
864863
freq = frequencies.get_to_timestamp_base(base)
865864

866-
base, mult = frequencies.get_freq_code(freq)
865+
base, mult = get_freq_code(freq)
867866
val = self.asfreq(freq, how)
868867

869868
dt64 = period_ordinal_to_dt64(val.ordinal, base)
870869
return Timestamp(dt64, tz=tz)
871870

872871
cdef _field(self, alias):
873-
base, mult = frequencies.get_freq_code(self.freq)
872+
base, mult = get_freq_code(self.freq)
874873
return get_period_field(alias, self.ordinal, base)
875874

876875
property year:
@@ -935,7 +934,7 @@ cdef class _Period(object):
935934
return self.freq.freqstr
936935

937936
def __repr__(self):
938-
base, mult = frequencies.get_freq_code(self.freq)
937+
base, mult = get_freq_code(self.freq)
939938
formatted = period_format(self.ordinal, base)
940939
return "Period('%s', '%s')" % (formatted, self.freqstr)
941940

@@ -946,7 +945,7 @@ cdef class _Period(object):
946945
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
947946
py2/py3.
948947
"""
949-
base, mult = frequencies.get_freq_code(self.freq)
948+
base, mult = get_freq_code(self.freq)
950949
formatted = period_format(self.ordinal, base)
951950
value = ("%s" % formatted)
952951
return value
@@ -1096,7 +1095,7 @@ cdef class _Period(object):
10961095
>>> a.strftime('%b. %d, %Y was a %A')
10971096
'Jan. 01, 2001 was a Monday'
10981097
"""
1099-
base, mult = frequencies.get_freq_code(self.freq)
1098+
base, mult = get_freq_code(self.freq)
11001099
return period_format(self.ordinal, base, fmt)
11011100

11021101

@@ -1161,10 +1160,10 @@ class Period(_Period):
11611160
ordinal = _ordinal_from_fields(year, month, quarter, day,
11621161
hour, minute, second, freq)
11631162

1164-
elif isinstance(value, Period):
1163+
elif is_period_object(value):
11651164
other = value
1166-
if freq is None or frequencies.get_freq_code(
1167-
freq) == frequencies.get_freq_code(other.freq):
1165+
if freq is None or get_freq_code(
1166+
freq) == get_freq_code(other.freq):
11681167
ordinal = other.ordinal
11691168
freq = other.freq
11701169
else:
@@ -1174,7 +1173,7 @@ class Period(_Period):
11741173
elif is_null_datetimelike(value) or value in tslib._nat_strings:
11751174
ordinal = iNaT
11761175

1177-
elif isinstance(value, compat.string_types) or lib.is_integer(value):
1176+
elif is_string_object(value) or lib.is_integer(value):
11781177
if lib.is_integer(value):
11791178
value = str(value)
11801179
value = value.upper()
@@ -1191,7 +1190,7 @@ class Period(_Period):
11911190
dt = value
11921191
if freq is None:
11931192
raise ValueError('Must supply freq for datetime value')
1194-
elif isinstance(value, np.datetime64):
1193+
elif util.is_datetime64_object(value):
11951194
dt = Timestamp(value)
11961195
if freq is None:
11971196
raise ValueError('Must supply freq for datetime value')
@@ -1204,7 +1203,7 @@ class Period(_Period):
12041203
raise ValueError(msg)
12051204

12061205
if ordinal is None:
1207-
base, mult = frequencies.get_freq_code(freq)
1206+
base, mult = get_freq_code(freq)
12081207
ordinal = get_period_ordinal(dt.year, dt.month, dt.day,
12091208
dt.hour, dt.minute, dt.second,
12101209
dt.microsecond, 0, base)
@@ -1214,7 +1213,7 @@ class Period(_Period):
12141213

12151214
def _ordinal_from_fields(year, month, quarter, day,
12161215
hour, minute, second, freq):
1217-
base, mult = frequencies.get_freq_code(freq)
1216+
base, mult = get_freq_code(freq)
12181217
if quarter is not None:
12191218
year, month = _quarter_to_myear(year, quarter, freq)
12201219

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

1230-
mnum = frequencies._month_numbers[
1231-
frequencies._get_rule_month(freq)] + 1
1229+
mnum = tslib._MONTH_NUMBERS[tslib._get_rule_month(freq)] + 1
12321230
month = (mnum + (quarter - 1) * 3) % 12 + 1
12331231
if month > mnum:
12341232
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)