From b2fa7cb829e1025c7544b4e83609ae2861337d21 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 30 Jun 2018 18:25:27 -0700 Subject: [PATCH] use liboffsets to_offset function --- pandas/_libs/tslib.pyx | 3 ++- pandas/_libs/tslibs/offsets.pxd | 3 +++ pandas/_libs/tslibs/period.pyx | 6 +++--- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/_libs/tslibs/timestamps.pyx | 4 +--- setup.py | 10 +++++++--- 6 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 pandas/_libs/tslibs/offsets.pxd diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 6588b5476e2b9..4c72e09a4851b 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -51,6 +51,8 @@ from tslibs.conversion import tz_convert_single from tslibs.nattype import NaT, nat_strings, iNaT from tslibs.nattype cimport checknull_with_nat, NPY_NAT +from tslibs.offsets cimport to_offset + from tslibs.timestamps cimport (create_timestamp_from_ts, _NS_UPPER_BOUND, _NS_LOWER_BOUND) from tslibs.timestamps import Timestamp @@ -118,7 +120,6 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, func_create = create_timestamp_from_ts if is_string_object(freq): - from pandas.tseries.frequencies import to_offset freq = to_offset(freq) elif box == "time": func_create = create_time_from_ts diff --git a/pandas/_libs/tslibs/offsets.pxd b/pandas/_libs/tslibs/offsets.pxd new file mode 100644 index 0000000000000..2829a27b9905c --- /dev/null +++ b/pandas/_libs/tslibs/offsets.pxd @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +cdef to_offset(object obj) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 6985d3b8df363..631ceab003f83 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -55,9 +55,9 @@ from parsing import parse_time_string, NAT_SENTINEL from resolution import Resolution from nattype import nat_strings, NaT, iNaT from nattype cimport _nat_scalar_rules, NPY_NAT +from offsets cimport to_offset from pandas.tseries import offsets -from pandas.tseries import frequencies cdef extern from "period_helper.h": @@ -1015,7 +1015,7 @@ cdef class _Period(object): code, stride = get_freq_code(freq) freq = get_freq_str(code, stride) - freq = frequencies.to_offset(freq) + freq = to_offset(freq) if freq.n <= 0: raise ValueError('Frequency must be positive, because it' @@ -1063,7 +1063,7 @@ cdef class _Period(object): if (PyDelta_Check(other) or util.is_timedelta64_object(other) or isinstance(other, offsets.Tick)): - offset = frequencies.to_offset(self.freq.rule_code) + offset = to_offset(self.freq.rule_code) if isinstance(offset, offsets.Tick): nanos = delta_to_nanoseconds(other) offset_nanos = delta_to_nanoseconds(offset) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index f68dc421a1ee9..27066af0be3b9 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -33,6 +33,7 @@ from np_datetime cimport (cmp_scalar, reverse_ops, td64_to_tdstruct, from nattype import nat_strings, NaT from nattype cimport checknull_with_nat, NPY_NAT +from offsets cimport to_offset # ---------------------------------------------------------------------- # Constants @@ -1050,7 +1051,6 @@ class Timedelta(_Timedelta): cdef: int64_t result, unit - from pandas.tseries.frequencies import to_offset unit = to_offset(freq).nanos result = unit * rounder(self.value / float(unit)) return Timedelta(result, unit='ns') diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 123ccebf83a56..54d29cea44555 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -30,6 +30,7 @@ from nattype cimport NPY_NAT from np_datetime import OutOfBoundsDatetime from np_datetime cimport (reverse_ops, cmp_scalar, check_dts_bounds, pandas_datetimestruct, dt64_to_dtstruct) +from offsets cimport to_offset from timedeltas import Timedelta from timedeltas cimport delta_to_nanoseconds from timezones cimport ( @@ -59,7 +60,6 @@ cdef inline object create_timestamp_from_ts(int64_t value, def round_ns(values, rounder, freq): - """ Applies rounding function at given frequency @@ -73,8 +73,6 @@ def round_ns(values, rounder, freq): ------- :obj:`ndarray` """ - - from pandas.tseries.frequencies import to_offset unit = to_offset(freq).nanos # GH21262 If the Timestamp is multiple of the freq str diff --git a/setup.py b/setup.py index 621655dd05dbc..75e2b18409e19 100755 --- a/setup.py +++ b/setup.py @@ -538,7 +538,8 @@ def pxd(name): '_libs/tslibs/ccalendar', '_libs/tslibs/timedeltas', '_libs/tslibs/timezones', - '_libs/tslibs/nattype'], + '_libs/tslibs/nattype', + '_libs/tslibs/offsets'], 'depends': tseries_depends + ['pandas/_libs/src/period_helper.h'], 'sources': np_datetime_sources + ['pandas/_libs/src/period_helper.c']}, '_libs.properties': { @@ -560,7 +561,8 @@ def pxd(name): '_libs/tslibs/timedeltas', '_libs/tslibs/timestamps', '_libs/tslibs/timezones', - '_libs/tslibs/nattype'], + '_libs/tslibs/nattype', + '_libs/tslibs/offsets'], 'depends': tseries_depends, 'sources': np_datetime_sources}, '_libs.tslibs.ccalendar': { @@ -619,7 +621,8 @@ def pxd(name): '_libs.tslibs.timedeltas': { 'pyxfile': '_libs/tslibs/timedeltas', 'pxdfiles': ['_libs/src/util', - '_libs/tslibs/nattype'], + '_libs/tslibs/nattype', + '_libs/tslibs/offsets'], 'depends': np_datetime_headers, 'sources': np_datetime_sources}, '_libs.tslibs.timestamps': { @@ -628,6 +631,7 @@ def pxd(name): '_libs/tslibs/ccalendar', '_libs/tslibs/conversion', '_libs/tslibs/nattype', + '_libs/tslibs/offsets', '_libs/tslibs/timedeltas', '_libs/tslibs/timezones'], 'depends': tseries_depends,