From 5fdd6f50ac5ac2de939ef5b849e4c41e27e623a3 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 16 Jun 2020 13:47:01 -0700 Subject: [PATCH] REF: remove libfrequencies (#34828) --- pandas/_libs/tslibs/frequencies.pxd | 1 - pandas/_libs/tslibs/frequencies.pyx | 40 ------------------- pandas/_libs/tslibs/period.pyx | 29 ++++++++++++-- pandas/core/arrays/period.py | 12 +++--- pandas/plotting/_matplotlib/timeseries.py | 2 +- .../tseries/frequencies/test_freq_code.py | 10 +++-- pandas/tests/tslibs/test_api.py | 1 - setup.py | 2 - 8 files changed, 38 insertions(+), 59 deletions(-) delete mode 100644 pandas/_libs/tslibs/frequencies.pxd delete mode 100644 pandas/_libs/tslibs/frequencies.pyx diff --git a/pandas/_libs/tslibs/frequencies.pxd b/pandas/_libs/tslibs/frequencies.pxd deleted file mode 100644 index b3ad6e6c19ee3..0000000000000 --- a/pandas/_libs/tslibs/frequencies.pxd +++ /dev/null @@ -1 +0,0 @@ -cpdef int get_to_timestamp_base(int base) diff --git a/pandas/_libs/tslibs/frequencies.pyx b/pandas/_libs/tslibs/frequencies.pyx deleted file mode 100644 index fd28240abd882..0000000000000 --- a/pandas/_libs/tslibs/frequencies.pyx +++ /dev/null @@ -1,40 +0,0 @@ - -from .dtypes import FreqGroup - -# ---------------------------------------------------------------------- - - -cpdef int get_to_timestamp_base(int base): - """ - Return frequency code group used for base of to_timestamp against - frequency code. - - Parameters - ---------- - base : int (member of FreqGroup) - - Returns - ------- - base : int - - Examples - -------- - # Return day freq code against longer freq than day - >>> get_to_timestamp_base(get_freq_code('D')[0]) - 6000 - >>> get_to_timestamp_base(get_freq_code('W')[0]) - 6000 - >>> get_to_timestamp_base(get_freq_code('M')[0]) - 6000 - - # Return second freq code against hour between second - >>> get_to_timestamp_base(get_freq_code('H')[0]) - 9000 - >>> get_to_timestamp_base(get_freq_code('S')[0]) - 9000 - """ - if base < FreqGroup.FR_BUS: - return FreqGroup.FR_DAY - elif FreqGroup.FR_HR <= base <= FreqGroup.FR_SEC: - return FreqGroup.FR_SEC - return base diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 30caddf81b6e8..a2250234dbd14 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -74,7 +74,6 @@ from pandas._libs.tslibs.dtypes cimport ( attrname_to_abbrevs, ) -from pandas._libs.tslibs.frequencies cimport get_to_timestamp_base from pandas._libs.tslibs.parsing cimport get_rule_month from pandas._libs.tslibs.parsing import parse_time_string from pandas._libs.tslibs.nattype cimport ( @@ -1478,7 +1477,30 @@ class IncompatibleFrequency(ValueError): pass -cdef class _Period: +cdef class PeriodMixin: + # Methods shared between Period and PeriodArray + + cpdef int _get_to_timestamp_base(self): + """ + Return frequency code group used for base of to_timestamp against + frequency code. + + Return day freq code against longer freq than day. + Return second freq code against hour between second. + + Returns + ------- + int + """ + base = self._dtype._dtype_code + if base < FR_BUS: + return FR_DAY + elif FR_HR <= base <= FR_SEC: + return FR_SEC + return base + + +cdef class _Period(PeriodMixin): cdef readonly: int64_t ordinal @@ -1734,8 +1756,7 @@ cdef class _Period: return endpoint - Timedelta(1, 'ns') if freq is None: - base = self._dtype._dtype_code - freq = get_to_timestamp_base(base) + freq = self._get_to_timestamp_base() base = freq else: freq = self._maybe_convert_freq(freq) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 0d866aa7eae26..7902dd0410910 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -9,17 +9,18 @@ NaTType, Timedelta, delta_to_nanoseconds, - frequencies as libfrequencies, iNaT, period as libperiod, to_offset, ) +from pandas._libs.tslibs.dtypes import FreqGroup from pandas._libs.tslibs.fields import isleapyear_arr from pandas._libs.tslibs.offsets import Tick, delta_to_tick from pandas._libs.tslibs.period import ( DIFFERENT_FREQ, IncompatibleFrequency, Period, + PeriodMixin, get_period_field_arr, period_asfreq_arr, ) @@ -61,7 +62,7 @@ def f(self): return property(f) -class PeriodArray(dtl.DatetimeLikeArrayMixin, dtl.DatelikeOps): +class PeriodArray(PeriodMixin, dtl.DatetimeLikeArrayMixin, dtl.DatelikeOps): """ Pandas ExtensionArray for storing Period data. @@ -440,8 +441,7 @@ def to_timestamp(self, freq=None, how="start"): return (self + self.freq).to_timestamp(how="start") - adjust if freq is None: - base = self.freq._period_dtype_code - freq = libfrequencies.get_to_timestamp_base(base) + freq = self._get_to_timestamp_base() base = freq else: freq = Period._maybe_convert_freq(freq) @@ -1027,11 +1027,11 @@ def _range_from_fields( if quarter is not None: if freq is None: freq = to_offset("Q") - base = libfrequencies.FreqGroup.FR_QTR + base = FreqGroup.FR_QTR else: freq = to_offset(freq) base = libperiod.freq_to_dtype_code(freq) - if base != libfrequencies.FreqGroup.FR_QTR: + if base != FreqGroup.FR_QTR: raise AssertionError("base must equal FR_QTR") year, quarter = _make_field_arrays(year, quarter) diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index fa8051954e435..8ffd30567b9ac 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -6,7 +6,7 @@ import numpy as np from pandas._libs.tslibs import Period, to_offset -from pandas._libs.tslibs.frequencies import FreqGroup +from pandas._libs.tslibs.dtypes import FreqGroup from pandas._typing import FrameOrSeriesUnion from pandas.core.dtypes.generic import ( diff --git a/pandas/tests/tseries/frequencies/test_freq_code.py b/pandas/tests/tseries/frequencies/test_freq_code.py index 5383c1ff1c2c9..20cadde45e7a0 100644 --- a/pandas/tests/tseries/frequencies/test_freq_code.py +++ b/pandas/tests/tseries/frequencies/test_freq_code.py @@ -1,8 +1,7 @@ import pytest -from pandas._libs.tslibs import Resolution, to_offset +from pandas._libs.tslibs import Period, Resolution, to_offset from pandas._libs.tslibs.dtypes import _attrname_to_abbrevs -from pandas._libs.tslibs.frequencies import get_to_timestamp_base @pytest.mark.parametrize( @@ -10,9 +9,12 @@ [("D", "D"), ("W", "D"), ("M", "D"), ("S", "S"), ("T", "S"), ("H", "S")], ) def test_get_to_timestamp_base(freqstr, exp_freqstr): - left_code = to_offset(freqstr)._period_dtype_code + off = to_offset(freqstr) + per = Period._from_ordinal(1, off) exp_code = to_offset(exp_freqstr)._period_dtype_code - assert get_to_timestamp_base(left_code) == exp_code + + result_code = per._get_to_timestamp_base() + assert result_code == exp_code @pytest.mark.parametrize( diff --git a/pandas/tests/tslibs/test_api.py b/pandas/tests/tslibs/test_api.py index b0c524a257684..a119db6c68635 100644 --- a/pandas/tests/tslibs/test_api.py +++ b/pandas/tests/tslibs/test_api.py @@ -11,7 +11,6 @@ def test_namespace(): "conversion", "dtypes", "fields", - "frequencies", "nattype", "np_datetime", "offsets", diff --git a/setup.py b/setup.py index 3caea5c5e79da..e9d305d831653 100755 --- a/setup.py +++ b/setup.py @@ -319,7 +319,6 @@ class CheckSDist(sdist_class): "pandas/_libs/tslibs/conversion.pyx", "pandas/_libs/tslibs/fields.pyx", "pandas/_libs/tslibs/offsets.pyx", - "pandas/_libs/tslibs/frequencies.pyx", "pandas/_libs/tslibs/resolution.pyx", "pandas/_libs/tslibs/parsing.pyx", "pandas/_libs/tslibs/tzconversion.pyx", @@ -615,7 +614,6 @@ def srcpath(name=None, suffix=".pyx", subdir="src"): "pyxfile": "_libs/tslibs/fields", "depends": tseries_depends, }, - "_libs.tslibs.frequencies": {"pyxfile": "_libs/tslibs/frequencies"}, "_libs.tslibs.nattype": {"pyxfile": "_libs/tslibs/nattype"}, "_libs.tslibs.np_datetime": { "pyxfile": "_libs/tslibs/np_datetime",