Skip to content

Commit 6db3e3c

Browse files
committed
rebase
2 parents e3995be + d6df8ea commit 6db3e3c

File tree

7 files changed

+44
-159
lines changed

7 files changed

+44
-159
lines changed

pandas/_libs/period.pyx

+27-29
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,7 +41,8 @@ from tslib cimport (
4141
_get_dst_info,
4242
_nat_scalar_rules)
4343

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
4546

4647
from pandas.tseries import offsets
4748
from pandas.tseries import frequencies
@@ -330,8 +331,6 @@ cdef list str_extra_fmts = ["^`AB`^", "^`CD`^", "^`EF`^",
330331
"^`GH`^", "^`IJ`^", "^`KL`^"]
331332

332333
cdef object _period_strftime(int64_t value, int freq, object fmt):
333-
import sys
334-
335334
cdef:
336335
Py_ssize_t i
337336
date_info dinfo
@@ -684,7 +683,7 @@ cdef class _Period(object):
684683
def _maybe_convert_freq(cls, object freq):
685684

686685
if isinstance(freq, (int, tuple)):
687-
code, stride = frequencies.get_freq_code(freq)
686+
code, stride = get_freq_code(freq)
688687
freq = frequencies._get_freq_str(code, stride)
689688

690689
freq = frequencies.to_offset(freq)
@@ -708,7 +707,7 @@ cdef class _Period(object):
708707
return self
709708

710709
def __richcmp__(self, other, op):
711-
if isinstance(other, Period):
710+
if is_period_object(other):
712711
if other.freq != self.freq:
713712
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
714713
raise IncompatibleFrequency(msg)
@@ -754,7 +753,7 @@ cdef class _Period(object):
754753
return NotImplemented
755754

756755
def __add__(self, other):
757-
if isinstance(self, Period):
756+
if is_period_object(self):
758757
if isinstance(other, (timedelta, np.timedelta64,
759758
offsets.DateOffset,
760759
Timedelta)):
@@ -766,13 +765,13 @@ cdef class _Period(object):
766765
return Period(ordinal=ordinal, freq=self.freq)
767766
else: # pragma: no cover
768767
return NotImplemented
769-
elif isinstance(other, Period):
768+
elif is_period_object(other):
770769
return other + self
771770
else:
772771
return NotImplemented
773772

774773
def __sub__(self, other):
775-
if isinstance(self, Period):
774+
if is_period_object(self):
776775
if isinstance(other, (timedelta, np.timedelta64,
777776
offsets.DateOffset,
778777
Timedelta)):
@@ -781,7 +780,7 @@ cdef class _Period(object):
781780
elif lib.is_integer(other):
782781
ordinal = self.ordinal - other * self.freq.n
783782
return Period(ordinal=ordinal, freq=self.freq)
784-
elif isinstance(other, Period):
783+
elif is_period_object(other):
785784
if other.freq != self.freq:
786785
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
787786
raise IncompatibleFrequency(msg)
@@ -790,7 +789,7 @@ cdef class _Period(object):
790789
return -other.__sub__(self)
791790
else: # pragma: no cover
792791
return NotImplemented
793-
elif isinstance(other, Period):
792+
elif is_period_object(other):
794793
if self is NaT:
795794
return NaT
796795
return NotImplemented
@@ -814,8 +813,8 @@ cdef class _Period(object):
814813
"""
815814
freq = self._maybe_convert_freq(freq)
816815
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)
819818

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

863862
if freq is None:
864-
base, mult = frequencies.get_freq_code(self.freq)
863+
base, mult = get_freq_code(self.freq)
865864
freq = frequencies.get_to_timestamp_base(base)
866865

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

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

873872
cdef _field(self, alias):
874-
base, mult = frequencies.get_freq_code(self.freq)
873+
base, mult = get_freq_code(self.freq)
875874
return get_period_field(alias, self.ordinal, base)
876875

877876
property year:
@@ -936,7 +935,7 @@ cdef class _Period(object):
936935
return self.freq.freqstr
937936

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

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

11031102

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

1165-
elif isinstance(value, Period):
1164+
elif is_period_object(value):
11661165
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):
11691168
ordinal = other.ordinal
11701169
freq = other.freq
11711170
else:
@@ -1175,7 +1174,7 @@ class Period(_Period):
11751174
elif is_null_datetimelike(value) or value in tslib._nat_strings:
11761175
ordinal = iNaT
11771176

1178-
elif isinstance(value, compat.string_types) or lib.is_integer(value):
1177+
elif is_string_object(value) or lib.is_integer(value):
11791178
if lib.is_integer(value):
11801179
value = str(value)
11811180
value = value.upper()
@@ -1194,7 +1193,7 @@ class Period(_Period):
11941193
dt = value
11951194
if freq is None:
11961195
raise ValueError('Must supply freq for datetime value')
1197-
elif isinstance(value, np.datetime64):
1196+
elif util.is_datetime64_object(value):
11981197
dt = Timestamp(value)
11991198
if freq is None:
12001199
raise ValueError('Must supply freq for datetime value')
@@ -1207,7 +1206,7 @@ class Period(_Period):
12071206
raise ValueError(msg)
12081207

12091208
if ordinal is None:
1210-
base, mult = frequencies.get_freq_code(freq)
1209+
base, mult = get_freq_code(freq)
12111210
ordinal = get_period_ordinal(dt.year, dt.month, dt.day,
12121211
dt.hour, dt.minute, dt.second,
12131212
dt.microsecond, 0, base)
@@ -1217,7 +1216,7 @@ class Period(_Period):
12171216

12181217
def _ordinal_from_fields(year, month, quarter, day,
12191218
hour, minute, second, freq):
1220-
base, mult = frequencies.get_freq_code(freq)
1219+
base, mult = get_freq_code(freq)
12211220
if quarter is not None:
12221221
year, month = _quarter_to_myear(year, quarter, freq)
12231222

@@ -1230,8 +1229,7 @@ def _quarter_to_myear(year, quarter, freq):
12301229
if quarter <= 0 or quarter > 4:
12311230
raise ValueError('Quarter must be 1 <= q <= 4')
12321231

1233-
mnum = frequencies._month_numbers[
1234-
frequencies._get_rule_month(freq)] + 1
1232+
mnum = tslib._MONTH_NUMBERS[tslib._get_rule_month(freq)] + 1
12351233
month = (mnum + (quarter - 1) * 3) % 12 + 1
12361234
if month > mnum:
12371235
year -= 1

pandas/_libs/src/inference.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ iNaT = util.get_nat()
88

99
cdef bint PY2 = sys.version_info[0] == 2
1010

11-
from pandas._libs.parsing import (
11+
from pandas._libs.tslibs.parsing import (
1212
try_parse_dates,
1313
try_parse_date_and_time,
1414
try_parse_year_month_day,

pandas/_libs/tslib.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ from khash cimport (
6767
kh_init_int64, kh_int64_t,
6868
kh_resize_int64, kh_get_int64)
6969

70-
from . import parsing # noqa
71-
from .parsing import ( # noqa
70+
from .tslibs import parsing # noqa
71+
from .tslibs.parsing import ( # noqa
7272
DateParseError,
7373
_format_is_iso,
7474
_DATEUTIL_LEXER_SPLIT,

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)