@@ -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,7 +41,8 @@ from tslib cimport (
41
41
_get_dst_info,
42
42
_nat_scalar_rules)
43
43
44
- from .parsing import parse_time_string, NAT_SENTINEL
44
+ from tslibs.parsing import parse_time_string, NAT_SENTINEL
45
+ from tslibs.frequencies cimport get_freq_code
45
46
46
47
from pandas.tseries import offsets
47
48
from pandas.tseries import frequencies
@@ -330,8 +331,6 @@ cdef list str_extra_fmts = ["^`AB`^", "^`CD`^", "^`EF`^",
330
331
" ^`GH`^" , " ^`IJ`^" , " ^`KL`^" ]
331
332
332
333
cdef object _period_strftime(int64_t value, int freq, object fmt):
333
- import sys
334
-
335
334
cdef:
336
335
Py_ssize_t i
337
336
date_info dinfo
@@ -684,7 +683,7 @@ cdef class _Period(object):
684
683
def _maybe_convert_freq (cls , object freq ):
685
684
686
685
if isinstance (freq, (int , tuple )):
687
- code, stride = frequencies. get_freq_code(freq)
686
+ code, stride = get_freq_code(freq)
688
687
freq = frequencies._get_freq_str(code, stride)
689
688
690
689
freq = frequencies.to_offset(freq)
@@ -708,7 +707,7 @@ cdef class _Period(object):
708
707
return self
709
708
710
709
def __richcmp__ (self , other , op ):
711
- if isinstance (other, Period ):
710
+ if is_period_object (other):
712
711
if other.freq != self .freq:
713
712
msg = _DIFFERENT_FREQ.format(self .freqstr, other.freqstr)
714
713
raise IncompatibleFrequency(msg)
@@ -754,7 +753,7 @@ cdef class _Period(object):
754
753
return NotImplemented
755
754
756
755
def __add__ (self , other ):
757
- if isinstance (self , Period ):
756
+ if is_period_object (self ):
758
757
if isinstance (other, (timedelta, np.timedelta64,
759
758
offsets.DateOffset,
760
759
Timedelta)):
@@ -766,13 +765,13 @@ cdef class _Period(object):
766
765
return Period(ordinal = ordinal, freq = self .freq)
767
766
else : # pragma: no cover
768
767
return NotImplemented
769
- elif isinstance (other, Period ):
768
+ elif is_period_object (other):
770
769
return other + self
771
770
else :
772
771
return NotImplemented
773
772
774
773
def __sub__ (self , other ):
775
- if isinstance (self , Period ):
774
+ if is_period_object (self ):
776
775
if isinstance (other, (timedelta, np.timedelta64,
777
776
offsets.DateOffset,
778
777
Timedelta)):
@@ -781,7 +780,7 @@ cdef class _Period(object):
781
780
elif lib.is_integer(other):
782
781
ordinal = self .ordinal - other * self .freq.n
783
782
return Period(ordinal = ordinal, freq = self .freq)
784
- elif isinstance (other, Period ):
783
+ elif is_period_object (other):
785
784
if other.freq != self .freq:
786
785
msg = _DIFFERENT_FREQ.format(self .freqstr, other.freqstr)
787
786
raise IncompatibleFrequency(msg)
@@ -790,7 +789,7 @@ cdef class _Period(object):
790
789
return - other.__sub__ (self )
791
790
else : # pragma: no cover
792
791
return NotImplemented
793
- elif isinstance (other, Period ):
792
+ elif is_period_object (other):
794
793
if self is NaT:
795
794
return NaT
796
795
return NotImplemented
@@ -814,8 +813,8 @@ cdef class _Period(object):
814
813
"""
815
814
freq = self ._maybe_convert_freq(freq)
816
815
how = _validate_end_alias(how)
817
- base1, mult1 = frequencies. get_freq_code(self .freq)
818
- base2, mult2 = frequencies. get_freq_code(freq)
816
+ base1, mult1 = get_freq_code(self .freq)
817
+ base2, mult2 = get_freq_code(freq)
819
818
820
819
# mult1 can't be negative or 0
821
820
end = how == ' E'
@@ -861,17 +860,17 @@ cdef class _Period(object):
861
860
how = _validate_end_alias(how)
862
861
863
862
if freq is None :
864
- base, mult = frequencies. get_freq_code(self .freq)
863
+ base, mult = get_freq_code(self .freq)
865
864
freq = frequencies.get_to_timestamp_base(base)
866
865
867
- base, mult = frequencies. get_freq_code(freq)
866
+ base, mult = get_freq_code(freq)
868
867
val = self .asfreq(freq, how)
869
868
870
869
dt64 = period_ordinal_to_dt64(val.ordinal, base)
871
870
return Timestamp(dt64, tz = tz)
872
871
873
872
cdef _field(self , alias):
874
- base, mult = frequencies. get_freq_code(self .freq)
873
+ base, mult = get_freq_code(self .freq)
875
874
return get_period_field(alias, self .ordinal, base)
876
875
877
876
property year :
@@ -936,7 +935,7 @@ cdef class _Period(object):
936
935
return self .freq.freqstr
937
936
938
937
def __repr__ (self ):
939
- base, mult = frequencies. get_freq_code(self .freq)
938
+ base, mult = get_freq_code(self .freq)
940
939
formatted = period_format(self .ordinal, base)
941
940
return " Period('%s ', '%s ')" % (formatted, self .freqstr)
942
941
@@ -947,7 +946,7 @@ cdef class _Period(object):
947
946
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
948
947
py2/py3.
949
948
"""
950
- base, mult = frequencies. get_freq_code(self .freq)
949
+ base, mult = get_freq_code(self .freq)
951
950
formatted = period_format(self .ordinal, base)
952
951
value = (" %s " % formatted)
953
952
return value
@@ -1097,7 +1096,7 @@ cdef class _Period(object):
1097
1096
>>> a.strftime('%b . %d , %Y was a %A ')
1098
1097
'Jan. 01, 2001 was a Monday'
1099
1098
"""
1100
- base, mult = frequencies. get_freq_code(self .freq)
1099
+ base, mult = get_freq_code(self .freq)
1101
1100
return period_format(self .ordinal, base, fmt)
1102
1101
1103
1102
@@ -1162,10 +1161,10 @@ class Period(_Period):
1162
1161
ordinal = _ordinal_from_fields(year, month, quarter, day,
1163
1162
hour, minute, second, freq)
1164
1163
1165
- elif isinstance (value, Period ):
1164
+ elif is_period_object (value):
1166
1165
other = value
1167
- if freq is None or frequencies. get_freq_code(
1168
- freq) == frequencies. get_freq_code(other.freq):
1166
+ if freq is None or get_freq_code(
1167
+ freq) == get_freq_code(other.freq):
1169
1168
ordinal = other.ordinal
1170
1169
freq = other.freq
1171
1170
else :
@@ -1175,7 +1174,7 @@ class Period(_Period):
1175
1174
elif is_null_datetimelike(value) or value in tslib._nat_strings:
1176
1175
ordinal = iNaT
1177
1176
1178
- elif isinstance (value, compat.string_types ) or lib.is_integer(value):
1177
+ elif is_string_object (value) or lib.is_integer(value):
1179
1178
if lib.is_integer(value):
1180
1179
value = str (value)
1181
1180
value = value.upper()
@@ -1194,7 +1193,7 @@ class Period(_Period):
1194
1193
dt = value
1195
1194
if freq is None :
1196
1195
raise ValueError (' Must supply freq for datetime value' )
1197
- elif isinstance (value, np.datetime64 ):
1196
+ elif util.is_datetime64_object (value):
1198
1197
dt = Timestamp(value)
1199
1198
if freq is None :
1200
1199
raise ValueError (' Must supply freq for datetime value' )
@@ -1207,7 +1206,7 @@ class Period(_Period):
1207
1206
raise ValueError (msg)
1208
1207
1209
1208
if ordinal is None :
1210
- base, mult = frequencies. get_freq_code(freq)
1209
+ base, mult = get_freq_code(freq)
1211
1210
ordinal = get_period_ordinal(dt.year, dt.month, dt.day,
1212
1211
dt.hour, dt.minute, dt.second,
1213
1212
dt.microsecond, 0 , base)
@@ -1217,7 +1216,7 @@ class Period(_Period):
1217
1216
1218
1217
def _ordinal_from_fields (year , month , quarter , day ,
1219
1218
hour , minute , second , freq ):
1220
- base, mult = frequencies. get_freq_code(freq)
1219
+ base, mult = get_freq_code(freq)
1221
1220
if quarter is not None :
1222
1221
year, month = _quarter_to_myear(year, quarter, freq)
1223
1222
@@ -1230,8 +1229,7 @@ def _quarter_to_myear(year, quarter, freq):
1230
1229
if quarter <= 0 or quarter > 4 :
1231
1230
raise ValueError (' Quarter must be 1 <= q <= 4' )
1232
1231
1233
- mnum = frequencies._month_numbers[
1234
- frequencies._get_rule_month(freq)] + 1
1232
+ mnum = tslib._MONTH_NUMBERS[tslib._get_rule_month(freq)] + 1
1235
1233
month = (mnum + (quarter - 1 ) * 3 ) % 12 + 1
1236
1234
if month > mnum:
1237
1235
year -= 1
0 commit comments