diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 700e9edd89bd0..e0c71b5609096 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -4,7 +4,8 @@ import numpy as np -from pandas._libs.tslibs import NaT, iNaT, period as libperiod +from pandas._libs.tslibs import ( + NaT, frequencies as libfrequencies, iNaT, period as libperiod) from pandas._libs.tslibs.fields import isleapyear_arr from pandas._libs.tslibs.period import ( DIFFERENT_FREQ, IncompatibleFrequency, Period, get_period_field_arr, @@ -31,7 +32,7 @@ def _field_accessor(name, alias, docstring=None): def f(self): - base, mult = frequencies.get_freq_code(self.freq) + base, mult = libfrequencies.get_freq_code(self.freq) result = get_period_field_arr(alias, self.asi8, base) return result @@ -348,12 +349,12 @@ def to_timestamp(self, freq=None, how='start'): return (self + self.freq).to_timestamp(how='start') - adjust if freq is None: - base, mult = frequencies.get_freq_code(self.freq) - freq = frequencies.get_to_timestamp_base(base) + base, mult = libfrequencies.get_freq_code(self.freq) + freq = libfrequencies.get_to_timestamp_base(base) else: freq = Period._maybe_convert_freq(freq) - base, mult = frequencies.get_freq_code(freq) + base, mult = libfrequencies.get_freq_code(freq) new_data = self.asfreq(freq, how=how) new_data = libperiod.periodarr_to_dt64arr(new_data.asi8, base) @@ -450,8 +451,8 @@ def asfreq(self, freq=None, how='E'): freq = Period._maybe_convert_freq(freq) - base1, mult1 = frequencies.get_freq_code(self.freq) - base2, mult2 = frequencies.get_freq_code(freq) + base1, mult1 = libfrequencies.get_freq_code(self.freq) + base2, mult2 = libfrequencies.get_freq_code(freq) asi8 = self.asi8 # mult1 can't be negative or 0 @@ -551,7 +552,7 @@ def _addsub_int_array( def _add_offset(self, other): assert not isinstance(other, Tick) - base = frequencies.get_base_alias(other.rule_code) + base = libfrequencies.get_base_alias(other.rule_code) if base != self.freq.rule_code: _raise_on_incompatible(self, other) @@ -855,7 +856,7 @@ def dt64arr_to_periodarr(data, freq, tz=None): if isinstance(data, (ABCIndexClass, ABCSeries)): data = data._values - base, mult = frequencies.get_freq_code(freq) + base, mult = libfrequencies.get_freq_code(freq) return libperiod.dt64arr_to_periodarr(data.view('i8'), base, tz), freq @@ -865,7 +866,7 @@ def _get_ordinal_range(start, end, periods, freq, mult=1): 'exactly two must be specified') if freq is not None: - _, mult = frequencies.get_freq_code(freq) + _, mult = libfrequencies.get_freq_code(freq) if start is not None: start = Period(start, freq) @@ -919,10 +920,10 @@ def _range_from_fields(year=None, month=None, quarter=None, day=None, if quarter is not None: if freq is None: freq = 'Q' - base = frequencies.FreqGroup.FR_QTR + base = libfrequencies.FreqGroup.FR_QTR else: - base, mult = frequencies.get_freq_code(freq) - if base != frequencies.FreqGroup.FR_QTR: + base, mult = libfrequencies.get_freq_code(freq) + if base != libfrequencies.FreqGroup.FR_QTR: raise AssertionError("base must equal FR_QTR") year, quarter = _make_field_arrays(year, quarter) @@ -931,7 +932,7 @@ def _range_from_fields(year=None, month=None, quarter=None, day=None, val = libperiod.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base) ordinals.append(val) else: - base, mult = frequencies.get_freq_code(freq) + base, mult = libfrequencies.get_freq_code(freq) arrays = _make_field_arrays(year, month, day, hour, minute, second) for y, mth, d, h, mn, s in compat.zip(*arrays): ordinals.append(libperiod.period_ordinal( diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 5e4dd2998a3be..a4bd7f9017eb4 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -5,7 +5,8 @@ import numpy as np from pandas._libs import index as libindex -from pandas._libs.tslibs import NaT, iNaT, resolution +from pandas._libs.tslibs import ( + NaT, frequencies as libfrequencies, iNaT, resolution) from pandas._libs.tslibs.period import ( DIFFERENT_FREQ, IncompatibleFrequency, Period) from pandas.util._decorators import Appender, Substitution, cache_readonly @@ -376,7 +377,7 @@ def _maybe_convert_timedelta(self, other): return delta elif isinstance(other, DateOffset): freqstr = other.rule_code - base = frequencies.get_base_alias(freqstr) + base = libfrequencies.get_base_alias(freqstr) if base == self.freq.rule_code: return other.n diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 25604b29f22f6..6822225273906 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -7,6 +7,7 @@ from pandas._libs import lib from pandas._libs.tslibs import NaT, Timestamp +from pandas._libs.tslibs.frequencies import is_subperiod, is_superperiod from pandas._libs.tslibs.period import IncompatibleFrequency import pandas.compat as compat from pandas.compat.numpy import function as nv @@ -28,7 +29,7 @@ from pandas.core.indexes.period import PeriodIndex from pandas.core.indexes.timedeltas import TimedeltaIndex, timedelta_range -from pandas.tseries.frequencies import is_subperiod, is_superperiod, to_offset +from pandas.tseries.frequencies import to_offset from pandas.tseries.offsets import ( DateOffset, Day, Nano, Tick, delta_to_nanoseconds) diff --git a/pandas/plotting/_converter.py b/pandas/plotting/_converter.py index 4c6b3b5132fec..aaa7aa04acf48 100644 --- a/pandas/plotting/_converter.py +++ b/pandas/plotting/_converter.py @@ -11,6 +11,7 @@ from pandas._libs import lib, tslibs from pandas._libs.tslibs import resolution +from pandas._libs.tslibs.frequencies import FreqGroup, get_freq import pandas.compat as compat from pandas.compat import lrange @@ -25,9 +26,6 @@ from pandas.core.indexes.period import Period, PeriodIndex, period_range import pandas.core.tools.datetimes as tools -import pandas.tseries.frequencies as frequencies -from pandas.tseries.frequencies import FreqGroup - # constants HOURS_PER_DAY = 24. MIN_PER_HOUR = 60. @@ -955,7 +953,7 @@ def _annual_finder(vmin, vmax, freq): def get_finder(freq): if isinstance(freq, compat.string_types): - freq = frequencies.get_freq(freq) + freq = get_freq(freq) fgroup = resolution.get_freq_group(freq) if fgroup == FreqGroup.FR_ANN: @@ -992,7 +990,7 @@ class TimeSeries_DateLocator(Locator): def __init__(self, freq, minor_locator=False, dynamic_mode=True, base=1, quarter=1, month=1, day=1, plot_obj=None): if isinstance(freq, compat.string_types): - freq = frequencies.get_freq(freq) + freq = get_freq(freq) self.freq = freq self.base = base (self.quarter, self.month, self.day) = (quarter, month, day) @@ -1073,7 +1071,7 @@ class TimeSeries_DateFormatter(Formatter): def __init__(self, freq, minor_locator=False, dynamic_mode=True, plot_obj=None): if isinstance(freq, compat.string_types): - freq = frequencies.get_freq(freq) + freq = get_freq(freq) self.format = None self.freq = freq self.locs = [] diff --git a/pandas/plotting/_timeseries.py b/pandas/plotting/_timeseries.py index 49249ae446747..51b0629005942 100644 --- a/pandas/plotting/_timeseries.py +++ b/pandas/plotting/_timeseries.py @@ -5,6 +5,8 @@ from matplotlib import pylab import numpy as np +from pandas._libs.tslibs.frequencies import ( + FreqGroup, get_base_alias, get_freq, is_subperiod, is_superperiod) from pandas._libs.tslibs.period import Period import pandas.compat as compat @@ -73,7 +75,7 @@ def _maybe_resample(series, ax, kwargs): series = series.to_period(freq=freq) if ax_freq is not None and freq != ax_freq: - if frequencies.is_superperiod(freq, ax_freq): # upsample input + if is_superperiod(freq, ax_freq): # upsample input series = series.copy() series.index = series.index.asfreq(ax_freq, how='s') freq = ax_freq @@ -82,7 +84,7 @@ def _maybe_resample(series, ax, kwargs): series = getattr(series.resample('D'), how)().dropna() series = getattr(series.resample(ax_freq), how)().dropna() freq = ax_freq - elif frequencies.is_subperiod(freq, ax_freq) or _is_sub(freq, ax_freq): + elif is_subperiod(freq, ax_freq) or _is_sub(freq, ax_freq): _upsample_others(ax, freq, kwargs) else: # pragma: no cover raise ValueError('Incompatible frequency conversion') @@ -90,13 +92,13 @@ def _maybe_resample(series, ax, kwargs): def _is_sub(f1, f2): - return ((f1.startswith('W') and frequencies.is_subperiod('D', f2)) or - (f2.startswith('W') and frequencies.is_subperiod(f1, 'D'))) + return ((f1.startswith('W') and is_subperiod('D', f2)) or + (f2.startswith('W') and is_subperiod(f1, 'D'))) def _is_sup(f1, f2): - return ((f1.startswith('W') and frequencies.is_superperiod('D', f2)) or - (f2.startswith('W') and frequencies.is_superperiod(f1, 'D'))) + return ((f1.startswith('W') and is_superperiod('D', f2)) or + (f2.startswith('W') and is_superperiod(f1, 'D'))) def _upsample_others(ax, freq, kwargs): @@ -209,7 +211,7 @@ def _get_freq(ax, series): if isinstance(freq, DateOffset): freq = freq.rule_code else: - freq = frequencies.get_base_alias(freq) + freq = get_base_alias(freq) freq = frequencies.get_period_alias(freq) return freq, ax_freq @@ -231,7 +233,7 @@ def _use_dynamic_x(ax, data): if isinstance(freq, DateOffset): freq = freq.rule_code else: - freq = frequencies.get_base_alias(freq) + freq = get_base_alias(freq) freq = frequencies.get_period_alias(freq) if freq is None: @@ -239,9 +241,9 @@ def _use_dynamic_x(ax, data): # hack this for 0.10.1, creating more technical debt...sigh if isinstance(data.index, ABCDatetimeIndex): - base = frequencies.get_freq(freq) + base = get_freq(freq) x = data.index - if (base <= frequencies.FreqGroup.FR_DAY): + if (base <= FreqGroup.FR_DAY): return x[:1].is_normalized return Period(x[0], freq).to_timestamp(tz=x.tz) == x[0] return True @@ -275,7 +277,7 @@ def _maybe_convert_index(ax, data): if freq is None: raise ValueError('Could not get frequency alias for plotting') - freq = frequencies.get_base_alias(freq) + freq = get_base_alias(freq) freq = frequencies.get_period_alias(freq) data = data.to_period(freq=freq) diff --git a/pandas/tests/indexes/datetimes/test_construction.py b/pandas/tests/indexes/datetimes/test_construction.py index 2768da0316aad..7c9ca9da89d53 100644 --- a/pandas/tests/indexes/datetimes/test_construction.py +++ b/pandas/tests/indexes/datetimes/test_construction.py @@ -366,8 +366,8 @@ def test_construction_with_ndarray(self): dates = [datetime(2013, 10, 7), datetime(2013, 10, 8), datetime(2013, 10, 9)] - data = DatetimeIndex(dates, freq=pd.tseries.frequencies.BDay()).values - result = DatetimeIndex(data, freq=pd.tseries.frequencies.BDay()) + data = DatetimeIndex(dates, freq=pd.offsets.BDay()).values + result = DatetimeIndex(data, freq=pd.offsets.BDay()) expected = DatetimeIndex(['2013-10-07', '2013-10-08', '2013-10-09'], diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 5ec3f69e55fde..e29974f56967f 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -292,7 +292,7 @@ def test_astype_categorical_to_other(self): tm.assert_series_equal(s.astype('category'), expected) tm.assert_series_equal(s.astype(CategoricalDtype()), expected) msg = (r"could not convert string to float: '(0 - 499|9500 - 9999)'|" - r"invalid literal for float\(\): 9500 - 9999") + r"invalid literal for float\(\): (0 - 499|9500 - 9999)") with pytest.raises(ValueError, match=msg): s.astype('float64') diff --git a/pandas/tests/tseries/test_frequencies.py b/pandas/tests/tseries/test_frequencies.py index c0a3e998dc2e0..eb4e63654b47b 100644 --- a/pandas/tests/tseries/test_frequencies.py +++ b/pandas/tests/tseries/test_frequencies.py @@ -3,10 +3,10 @@ import numpy as np import pytest -from pandas._libs.tslibs import resolution +from pandas._libs.tslibs import frequencies as libfrequencies, resolution from pandas._libs.tslibs.ccalendar import MONTHS from pandas._libs.tslibs.frequencies import ( - INVALID_FREQ_ERR_MSG, _period_code_map) + INVALID_FREQ_ERR_MSG, FreqGroup, _period_code_map, get_freq, get_freq_code) import pandas.compat as compat from pandas.compat import is_platform_windows, range @@ -277,20 +277,20 @@ def test_rule_aliases(): class TestFrequencyCode(object): def test_freq_code(self): - assert frequencies.get_freq('A') == 1000 - assert frequencies.get_freq('3A') == 1000 - assert frequencies.get_freq('-1A') == 1000 + assert get_freq('A') == 1000 + assert get_freq('3A') == 1000 + assert get_freq('-1A') == 1000 - assert frequencies.get_freq('Y') == 1000 - assert frequencies.get_freq('3Y') == 1000 - assert frequencies.get_freq('-1Y') == 1000 + assert get_freq('Y') == 1000 + assert get_freq('3Y') == 1000 + assert get_freq('-1Y') == 1000 - assert frequencies.get_freq('W') == 4000 - assert frequencies.get_freq('W-MON') == 4001 - assert frequencies.get_freq('W-FRI') == 4005 + assert get_freq('W') == 4000 + assert get_freq('W-MON') == 4001 + assert get_freq('W-FRI') == 4005 for freqstr, code in compat.iteritems(_period_code_map): - result = frequencies.get_freq(freqstr) + result = get_freq(freqstr) assert result == code result = resolution.get_freq_group(freqstr) @@ -324,24 +324,24 @@ def test_freq_group(self): assert resolution.get_freq_group(offsets.Week(weekday=5)) == 4000 def test_get_to_timestamp_base(self): - tsb = frequencies.get_to_timestamp_base - - assert (tsb(frequencies.get_freq_code('D')[0]) == - frequencies.get_freq_code('D')[0]) - assert (tsb(frequencies.get_freq_code('W')[0]) == - frequencies.get_freq_code('D')[0]) - assert (tsb(frequencies.get_freq_code('M')[0]) == - frequencies.get_freq_code('D')[0]) - - assert (tsb(frequencies.get_freq_code('S')[0]) == - frequencies.get_freq_code('S')[0]) - assert (tsb(frequencies.get_freq_code('T')[0]) == - frequencies.get_freq_code('S')[0]) - assert (tsb(frequencies.get_freq_code('H')[0]) == - frequencies.get_freq_code('S')[0]) + tsb = libfrequencies.get_to_timestamp_base + + assert (tsb(get_freq_code('D')[0]) == + get_freq_code('D')[0]) + assert (tsb(get_freq_code('W')[0]) == + get_freq_code('D')[0]) + assert (tsb(get_freq_code('M')[0]) == + get_freq_code('D')[0]) + + assert (tsb(get_freq_code('S')[0]) == + get_freq_code('S')[0]) + assert (tsb(get_freq_code('T')[0]) == + get_freq_code('S')[0]) + assert (tsb(get_freq_code('H')[0]) == + get_freq_code('S')[0]) def test_freq_to_reso(self): - Reso = frequencies.Resolution + Reso = resolution.Resolution assert Reso.get_str_from_freq('A') == 'year' assert Reso.get_str_from_freq('Q') == 'quarter' @@ -365,7 +365,7 @@ def test_freq_to_reso(self): def test_resolution_bumping(self): # see gh-14378 - Reso = frequencies.Resolution + Reso = resolution.Resolution assert Reso.get_stride_from_decimal(1.5, 'T') == (90, 'S') assert Reso.get_stride_from_decimal(62.4, 'T') == (3744, 'S') @@ -384,63 +384,63 @@ def test_resolution_bumping(self): def test_get_freq_code(self): # frequency str - assert (frequencies.get_freq_code('A') == - (frequencies.get_freq('A'), 1)) - assert (frequencies.get_freq_code('3D') == - (frequencies.get_freq('D'), 3)) - assert (frequencies.get_freq_code('-2M') == - (frequencies.get_freq('M'), -2)) + assert (get_freq_code('A') == + (get_freq('A'), 1)) + assert (get_freq_code('3D') == + (get_freq('D'), 3)) + assert (get_freq_code('-2M') == + (get_freq('M'), -2)) # tuple - assert (frequencies.get_freq_code(('D', 1)) == - (frequencies.get_freq('D'), 1)) - assert (frequencies.get_freq_code(('A', 3)) == - (frequencies.get_freq('A'), 3)) - assert (frequencies.get_freq_code(('M', -2)) == - (frequencies.get_freq('M'), -2)) + assert (get_freq_code(('D', 1)) == + (get_freq('D'), 1)) + assert (get_freq_code(('A', 3)) == + (get_freq('A'), 3)) + assert (get_freq_code(('M', -2)) == + (get_freq('M'), -2)) # numeric tuple - assert frequencies.get_freq_code((1000, 1)) == (1000, 1) + assert get_freq_code((1000, 1)) == (1000, 1) # offsets - assert (frequencies.get_freq_code(offsets.Day()) == - (frequencies.get_freq('D'), 1)) - assert (frequencies.get_freq_code(offsets.Day(3)) == - (frequencies.get_freq('D'), 3)) - assert (frequencies.get_freq_code(offsets.Day(-2)) == - (frequencies.get_freq('D'), -2)) - - assert (frequencies.get_freq_code(offsets.MonthEnd()) == - (frequencies.get_freq('M'), 1)) - assert (frequencies.get_freq_code(offsets.MonthEnd(3)) == - (frequencies.get_freq('M'), 3)) - assert (frequencies.get_freq_code(offsets.MonthEnd(-2)) == - (frequencies.get_freq('M'), -2)) - - assert (frequencies.get_freq_code(offsets.Week()) == - (frequencies.get_freq('W'), 1)) - assert (frequencies.get_freq_code(offsets.Week(3)) == - (frequencies.get_freq('W'), 3)) - assert (frequencies.get_freq_code(offsets.Week(-2)) == - (frequencies.get_freq('W'), -2)) + assert (get_freq_code(offsets.Day()) == + (get_freq('D'), 1)) + assert (get_freq_code(offsets.Day(3)) == + (get_freq('D'), 3)) + assert (get_freq_code(offsets.Day(-2)) == + (get_freq('D'), -2)) + + assert (get_freq_code(offsets.MonthEnd()) == + (get_freq('M'), 1)) + assert (get_freq_code(offsets.MonthEnd(3)) == + (get_freq('M'), 3)) + assert (get_freq_code(offsets.MonthEnd(-2)) == + (get_freq('M'), -2)) + + assert (get_freq_code(offsets.Week()) == + (get_freq('W'), 1)) + assert (get_freq_code(offsets.Week(3)) == + (get_freq('W'), 3)) + assert (get_freq_code(offsets.Week(-2)) == + (get_freq('W'), -2)) # Monday is weekday=0 - assert (frequencies.get_freq_code(offsets.Week(weekday=1)) == - (frequencies.get_freq('W-TUE'), 1)) - assert (frequencies.get_freq_code(offsets.Week(3, weekday=0)) == - (frequencies.get_freq('W-MON'), 3)) - assert (frequencies.get_freq_code(offsets.Week(-2, weekday=4)) == - (frequencies.get_freq('W-FRI'), -2)) + assert (get_freq_code(offsets.Week(weekday=1)) == + (get_freq('W-TUE'), 1)) + assert (get_freq_code(offsets.Week(3, weekday=0)) == + (get_freq('W-MON'), 3)) + assert (get_freq_code(offsets.Week(-2, weekday=4)) == + (get_freq('W-FRI'), -2)) def test_frequency_misc(self): assert (resolution.get_freq_group('T') == - frequencies.FreqGroup.FR_MIN) + FreqGroup.FR_MIN) - code, stride = frequencies.get_freq_code(offsets.Hour()) - assert code == frequencies.FreqGroup.FR_HR + code, stride = get_freq_code(offsets.Hour()) + assert code == FreqGroup.FR_HR - code, stride = frequencies.get_freq_code((5, 'T')) - assert code == frequencies.FreqGroup.FR_MIN + code, stride = get_freq_code((5, 'T')) + assert code == FreqGroup.FR_MIN assert stride == 5 offset = offsets.Hour() @@ -452,7 +452,7 @@ def test_frequency_misc(self): assert result == expected with pytest.raises(ValueError, match='Invalid frequency'): - frequencies.get_freq_code((5, 'baz')) + get_freq_code((5, 'baz')) with pytest.raises(ValueError, match='Invalid frequency'): frequencies.to_offset('100foo') diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 8cdec31d7ce8a..c454db3bbdffc 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -11,10 +11,7 @@ from pandas._libs.tslibs.conversion import tz_convert from pandas._libs.tslibs.fields import build_field_sarray import pandas._libs.tslibs.frequencies as libfreqs -from pandas._libs.tslibs.frequencies import ( # noqa, semi-public API - FreqGroup, get_base_alias, get_freq, get_freq_code, get_to_timestamp_base, - is_subperiod, is_superperiod) -from pandas._libs.tslibs.offsets import _offset_to_period_map # noqa:E402 +from pandas._libs.tslibs.offsets import _offset_to_period_map import pandas._libs.tslibs.resolution as libresolution from pandas._libs.tslibs.resolution import Resolution from pandas._libs.tslibs.timezones import UTC @@ -28,19 +25,8 @@ from pandas.core.algorithms import unique -from pandas.tseries.offsets import ( # noqa - BDay, BMonthBegin, BMonthEnd, BQuarterBegin, BQuarterEnd, BYearBegin, - BYearEnd, CDay, DateOffset, Day, Hour, Micro, Milli, Minute, MonthBegin, - MonthEnd, Nano, QuarterBegin, QuarterEnd, Second, Week, YearBegin, YearEnd, - prefix_mapping) - -RESO_NS = 0 -RESO_US = 1 -RESO_MS = 2 -RESO_SEC = 3 -RESO_MIN = 4 -RESO_HR = 5 -RESO_DAY = 6 +from pandas.tseries.offsets import ( + DateOffset, Day, Hour, Micro, Milli, Minute, Nano, Second, prefix_mapping) _ONE_MICRO = 1000 _ONE_MILLI = (_ONE_MICRO * 1000) @@ -52,11 +38,6 @@ # --------------------------------------------------------------------- # Offset names ("time rules") and related functions -try: - cday = CDay() -except NotImplementedError: - cday = None - #: cache of previously seen offsets _offset_map = {} @@ -216,8 +197,6 @@ def get_offset(name): return _offset_map[name] -getOffset = get_offset - # --------------------------------------------------------------------- # Period codes @@ -314,7 +293,7 @@ def is_unique(self): def is_unique_asi8(self): return len(self.deltas_asi8) == 1 - def get_freq(self): # noqa:F811 + def get_freq(self): """ Find the appropriate frequency string to describe the inferred frequency of self.values