@@ -10,17 +10,16 @@ from cpython cimport (
10
10
from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray,
11
11
NPY_INT64, NPY_DATETIME, NPY_TIMEDELTA)
12
12
import numpy as np
13
+ import_array()
13
14
14
15
from libc.stdlib cimport free
15
16
16
- from pandas import compat
17
17
from pandas.compat import PY2
18
18
19
19
cimport cython
20
20
21
21
from datetime cimport (
22
22
is_leapyear,
23
- PyDateTime_IMPORT,
24
23
pandas_datetimestruct,
25
24
pandas_datetimestruct_to_datetime,
26
25
pandas_datetime_to_datetimestruct,
@@ -29,6 +28,7 @@ from datetime cimport (
29
28
30
29
31
30
cimport util, lib
31
+ from util cimport is_period_object, is_string_object
32
32
33
33
from lib cimport is_null_datetimelike, is_period
34
34
from pandas._libs import tslib, lib
@@ -41,6 +41,8 @@ from tslib cimport (
41
41
_get_dst_info,
42
42
_nat_scalar_rules)
43
43
44
+ from tslibs.frequencies cimport get_freq_code
45
+
44
46
from pandas.tseries import offsets
45
47
from pandas.core.tools.datetimes import parse_time_string
46
48
from pandas.tseries import frequencies
@@ -329,8 +331,6 @@ cdef list str_extra_fmts = ["^`AB`^", "^`CD`^", "^`EF`^",
329
331
" ^`GH`^" , " ^`IJ`^" , " ^`KL`^" ]
330
332
331
333
cdef object _period_strftime(int64_t value, int freq, object fmt):
332
- import sys
333
-
334
334
cdef:
335
335
Py_ssize_t i
336
336
date_info dinfo
@@ -683,7 +683,7 @@ cdef class _Period(object):
683
683
def _maybe_convert_freq (cls , object freq ):
684
684
685
685
if isinstance (freq, (int , tuple )):
686
- code, stride = frequencies. get_freq_code(freq)
686
+ code, stride = get_freq_code(freq)
687
687
freq = frequencies._get_freq_str(code, stride)
688
688
689
689
freq = frequencies.to_offset(freq)
@@ -707,7 +707,7 @@ cdef class _Period(object):
707
707
return self
708
708
709
709
def __richcmp__ (self , other , op ):
710
- if isinstance (other, Period ):
710
+ if is_period_object (other):
711
711
if other.freq != self .freq:
712
712
msg = _DIFFERENT_FREQ.format(self .freqstr, other.freqstr)
713
713
raise IncompatibleFrequency(msg)
@@ -753,7 +753,7 @@ cdef class _Period(object):
753
753
return NotImplemented
754
754
755
755
def __add__ (self , other ):
756
- if isinstance (self , Period ):
756
+ if is_period_object (self ):
757
757
if isinstance (other, (timedelta, np.timedelta64,
758
758
offsets.DateOffset,
759
759
Timedelta)):
@@ -765,13 +765,13 @@ cdef class _Period(object):
765
765
return Period(ordinal = ordinal, freq = self .freq)
766
766
else : # pragma: no cover
767
767
return NotImplemented
768
- elif isinstance (other, Period ):
768
+ elif is_period_object (other):
769
769
return other + self
770
770
else :
771
771
return NotImplemented
772
772
773
773
def __sub__ (self , other ):
774
- if isinstance (self , Period ):
774
+ if is_period_object (self ):
775
775
if isinstance (other, (timedelta, np.timedelta64,
776
776
offsets.DateOffset,
777
777
Timedelta)):
@@ -780,7 +780,7 @@ cdef class _Period(object):
780
780
elif lib.is_integer(other):
781
781
ordinal = self .ordinal - other * self .freq.n
782
782
return Period(ordinal = ordinal, freq = self .freq)
783
- elif isinstance (other, Period ):
783
+ elif is_period_object (other):
784
784
if other.freq != self .freq:
785
785
msg = _DIFFERENT_FREQ.format(self .freqstr, other.freqstr)
786
786
raise IncompatibleFrequency(msg)
@@ -789,7 +789,7 @@ cdef class _Period(object):
789
789
return - other.__sub__ (self )
790
790
else : # pragma: no cover
791
791
return NotImplemented
792
- elif isinstance (other, Period ):
792
+ elif is_period_object (other):
793
793
if self is NaT:
794
794
return NaT
795
795
return NotImplemented
@@ -813,8 +813,8 @@ cdef class _Period(object):
813
813
"""
814
814
freq = self ._maybe_convert_freq(freq)
815
815
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)
818
818
819
819
# mult1 can't be negative or 0
820
820
end = how == ' E'
@@ -860,17 +860,17 @@ cdef class _Period(object):
860
860
how = _validate_end_alias(how)
861
861
862
862
if freq is None :
863
- base, mult = frequencies. get_freq_code(self .freq)
863
+ base, mult = get_freq_code(self .freq)
864
864
freq = frequencies.get_to_timestamp_base(base)
865
865
866
- base, mult = frequencies. get_freq_code(freq)
866
+ base, mult = get_freq_code(freq)
867
867
val = self .asfreq(freq, how)
868
868
869
869
dt64 = period_ordinal_to_dt64(val.ordinal, base)
870
870
return Timestamp(dt64, tz = tz)
871
871
872
872
cdef _field(self , alias):
873
- base, mult = frequencies. get_freq_code(self .freq)
873
+ base, mult = get_freq_code(self .freq)
874
874
return get_period_field(alias, self .ordinal, base)
875
875
876
876
property year :
@@ -935,7 +935,7 @@ cdef class _Period(object):
935
935
return self .freq.freqstr
936
936
937
937
def __repr__ (self ):
938
- base, mult = frequencies. get_freq_code(self .freq)
938
+ base, mult = get_freq_code(self .freq)
939
939
formatted = period_format(self .ordinal, base)
940
940
return " Period('%s ', '%s ')" % (formatted, self .freqstr)
941
941
@@ -946,7 +946,7 @@ cdef class _Period(object):
946
946
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
947
947
py2/py3.
948
948
"""
949
- base, mult = frequencies. get_freq_code(self .freq)
949
+ base, mult = get_freq_code(self .freq)
950
950
formatted = period_format(self .ordinal, base)
951
951
value = (" %s " % formatted)
952
952
return value
@@ -1096,7 +1096,7 @@ cdef class _Period(object):
1096
1096
>>> a.strftime('%b . %d , %Y was a %A ')
1097
1097
'Jan. 01, 2001 was a Monday'
1098
1098
"""
1099
- base, mult = frequencies. get_freq_code(self .freq)
1099
+ base, mult = get_freq_code(self .freq)
1100
1100
return period_format(self .ordinal, base, fmt)
1101
1101
1102
1102
@@ -1161,10 +1161,10 @@ class Period(_Period):
1161
1161
ordinal = _ordinal_from_fields(year, month, quarter, day,
1162
1162
hour, minute, second, freq)
1163
1163
1164
- elif isinstance (value, Period ):
1164
+ elif is_period_object (value):
1165
1165
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):
1168
1168
ordinal = other.ordinal
1169
1169
freq = other.freq
1170
1170
else :
@@ -1174,7 +1174,7 @@ class Period(_Period):
1174
1174
elif is_null_datetimelike(value) or value in tslib._nat_strings:
1175
1175
ordinal = iNaT
1176
1176
1177
- elif isinstance (value, compat.string_types ) or lib.is_integer(value):
1177
+ elif is_string_object (value) or lib.is_integer(value):
1178
1178
if lib.is_integer(value):
1179
1179
value = str (value)
1180
1180
value = value.upper()
@@ -1191,7 +1191,7 @@ class Period(_Period):
1191
1191
dt = value
1192
1192
if freq is None :
1193
1193
raise ValueError (' Must supply freq for datetime value' )
1194
- elif isinstance (value, np.datetime64 ):
1194
+ elif util.is_datetime64_object (value):
1195
1195
dt = Timestamp(value)
1196
1196
if freq is None :
1197
1197
raise ValueError (' Must supply freq for datetime value' )
@@ -1204,7 +1204,7 @@ class Period(_Period):
1204
1204
raise ValueError (msg)
1205
1205
1206
1206
if ordinal is None :
1207
- base, mult = frequencies. get_freq_code(freq)
1207
+ base, mult = get_freq_code(freq)
1208
1208
ordinal = get_period_ordinal(dt.year, dt.month, dt.day,
1209
1209
dt.hour, dt.minute, dt.second,
1210
1210
dt.microsecond, 0 , base)
@@ -1214,7 +1214,7 @@ class Period(_Period):
1214
1214
1215
1215
def _ordinal_from_fields (year , month , quarter , day ,
1216
1216
hour , minute , second , freq ):
1217
- base, mult = frequencies. get_freq_code(freq)
1217
+ base, mult = get_freq_code(freq)
1218
1218
if quarter is not None :
1219
1219
year, month = _quarter_to_myear(year, quarter, freq)
1220
1220
@@ -1227,8 +1227,7 @@ def _quarter_to_myear(year, quarter, freq):
1227
1227
if quarter <= 0 or quarter > 4 :
1228
1228
raise ValueError (' Quarter must be 1 <= q <= 4' )
1229
1229
1230
- mnum = frequencies._month_numbers[
1231
- frequencies._get_rule_month(freq)] + 1
1230
+ mnum = tslib._MONTH_NUMBERS[tslib._get_rule_month(freq)] + 1
1232
1231
month = (mnum + (quarter - 1 ) * 3 ) % 12 + 1
1233
1232
if month > mnum:
1234
1233
year -= 1
0 commit comments