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