diff --git a/pandas/_libs/tslibs/frequencies.pyx b/pandas/_libs/tslibs/frequencies.pyx index abaf8cad09bdb..56dcd5c405c6b 100644 --- a/pandas/_libs/tslibs/frequencies.pyx +++ b/pandas/_libs/tslibs/frequencies.pyx @@ -20,7 +20,7 @@ opattern = re.compile( r'([+\-]?\d*|[+\-]?\d*\.\d*)\s*([A-Za-z]+([\-][\dA-Za-z\-]+)?)' ) -_INVALID_FREQ_ERROR = "Invalid frequency: {0}" +INVALID_FREQ_ERR_MSG = "Invalid frequency: {0}" # --------------------------------------------------------------------- # Period codes @@ -220,7 +220,7 @@ cpdef _period_str_to_code(freqstr): try: return _period_code_map[freqstr] except KeyError: - raise ValueError(_INVALID_FREQ_ERROR.format(freqstr)) + raise ValueError(INVALID_FREQ_ERR_MSG.format(freqstr)) cpdef str get_freq_str(base, mult=1): diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 6e5794ef41757..e39731cfb66f4 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -896,7 +896,7 @@ def extract_ordinals(ndarray[object] values, freq): ordinals[i] = p.ordinal if p.freqstr != freqstr: - msg = _DIFFERENT_FREQ_INDEX.format(freqstr, p.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(freqstr, p.freqstr) raise IncompatibleFrequency(msg) except AttributeError: @@ -986,8 +986,8 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps, _DIFFERENT_FREQ = "Input has different freq={1} from Period(freq={0})" -_DIFFERENT_FREQ_INDEX = ("Input has different freq={1} " - "from PeriodIndex(freq={0})") +DIFFERENT_FREQ_INDEX = ("Input has different freq={1} " + "from PeriodIndex(freq={0})") class IncompatibleFrequency(ValueError): diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 225ccbf590779..08d4de972bc06 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -15,7 +15,6 @@ from pandas.core.accessor import PandasDelegate from pandas.core.base import NoNewAttributesMixin, PandasObject from pandas.core.indexes.datetimes import DatetimeIndex -from pandas._libs.tslibs.period import IncompatibleFrequency # noqa from pandas.core.indexes.period import PeriodIndex from pandas.core.indexes.timedeltas import TimedeltaIndex from pandas.core.algorithms import take_1d diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 328c51aae1807..0ca8af335fe4a 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -14,7 +14,7 @@ from pandas._libs import lib, iNaT, NaT, Timedelta from pandas._libs.tslibs.period import (Period, IncompatibleFrequency, - _DIFFERENT_FREQ_INDEX) + DIFFERENT_FREQ_INDEX) from pandas._libs.tslibs.timestamps import round_ns from pandas.core.dtypes.common import ( @@ -732,7 +732,7 @@ def _sub_period_array(self, other): if not len(self) == len(other): raise ValueError("cannot subtract indices of unequal length") if self.freq != other.freq: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) new_values = checked_add_with_arr(self.asi8, -other.asi8, diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 1257266025c03..c9525b9c30270 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -30,6 +30,7 @@ from pandas._libs import tslib, index as libindex from pandas._libs.tslibs.period import (Period, IncompatibleFrequency, get_period_field_arr, + DIFFERENT_FREQ_INDEX, _validate_end_alias, _quarter_to_myear) from pandas._libs.tslibs.fields import isleapyear_arr from pandas._libs.tslibs import resolution, period @@ -71,9 +72,6 @@ def dt64arr_to_periodarr(data, freq, tz): # --- Period index sketch -_DIFFERENT_FREQ_INDEX = period._DIFFERENT_FREQ_INDEX - - def _period_index_cmp(opname, cls): """ Wrap comparison operations to convert Period-like to PeriodDtype @@ -84,13 +82,13 @@ def wrapper(self, other): op = getattr(self._ndarray_values, opname) if isinstance(other, Period): if other.freq != self.freq: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) result = op(other.ordinal) elif isinstance(other, PeriodIndex): if other.freq != self.freq: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) result = op(other._ndarray_values) @@ -523,7 +521,7 @@ def astype(self, dtype, copy=True, how='start'): def searchsorted(self, value, side='left', sorter=None): if isinstance(value, Period): if value.freq != self.freq: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, value.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, value.freqstr) raise IncompatibleFrequency(msg) value = value.ordinal elif isinstance(value, compat.string_types): @@ -706,7 +704,7 @@ def _maybe_convert_timedelta(self, other): base = frequencies.get_base_alias(freqstr) if base == self.freq.rule_code: return other.n - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) elif is_integer(other): # integer is passed to .shift via @@ -721,7 +719,7 @@ def _add_offset(self, other): assert not isinstance(other, Tick) base = frequencies.get_base_alias(other.rule_code) if base != self.freq.rule_code: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) return self.shift(other.n) @@ -753,7 +751,7 @@ def _sub_period(self, other): # If the operation is well-defined, we return an object-Index # of DateOffsets. Null entries are filled with pd.NaT if self.freq != other.freq: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) asi8 = self.asi8 @@ -837,7 +835,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None): target = _ensure_index(target) if hasattr(target, 'freq') and target.freq != self.freq: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, target.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, target.freqstr) raise IncompatibleFrequency(msg) if isinstance(target, PeriodIndex): @@ -1010,7 +1008,7 @@ def _assert_can_do_setop(self, other): raise ValueError('can only call with other PeriodIndex-ed objects') if self.freq != other.freq: - msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) + msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) def _wrap_union_result(self, other, result): diff --git a/pandas/tests/indexes/datetimes/test_scalar_compat.py b/pandas/tests/indexes/datetimes/test_scalar_compat.py index 801dcb91b124e..f0442d9d40ef1 100644 --- a/pandas/tests/indexes/datetimes/test_scalar_compat.py +++ b/pandas/tests/indexes/datetimes/test_scalar_compat.py @@ -101,7 +101,7 @@ def test_round(self, tz): tm.assert_index_equal(rng.round(freq='H'), expected_rng) assert elt.round(freq='H') == expected_elt - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with tm.assert_raises_regex(ValueError, msg): rng.round(freq='foo') with tm.assert_raises_regex(ValueError, msg): diff --git a/pandas/tests/indexes/period/test_tools.py b/pandas/tests/indexes/period/test_tools.py index 38c6f257b2206..16b558916df2d 100644 --- a/pandas/tests/indexes/period/test_tools.py +++ b/pandas/tests/indexes/period/test_tools.py @@ -167,7 +167,7 @@ def test_to_period_monthish(self): prng = rng.to_period() assert prng.freq == 'M' - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with tm.assert_raises_regex(ValueError, msg): date_range('01-Jan-2012', periods=8, freq='EOM') diff --git a/pandas/tests/indexes/timedeltas/test_scalar_compat.py b/pandas/tests/indexes/timedeltas/test_scalar_compat.py index 7d97e1fadea30..e571ec2ccf20b 100644 --- a/pandas/tests/indexes/timedeltas/test_scalar_compat.py +++ b/pandas/tests/indexes/timedeltas/test_scalar_compat.py @@ -50,7 +50,7 @@ def test_tdi_round(self): tm.assert_index_equal(td.round(freq='H'), expected_rng) assert elt.round(freq='H') == expected_elt - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with tm.assert_raises_regex(ValueError, msg): td.round(freq='foo') with tm.assert_raises_regex(ValueError, msg): diff --git a/pandas/tests/scalar/period/test_asfreq.py b/pandas/tests/scalar/period/test_asfreq.py index 474d19809b03c..8fde9a417f3b7 100644 --- a/pandas/tests/scalar/period/test_asfreq.py +++ b/pandas/tests/scalar/period/test_asfreq.py @@ -325,13 +325,13 @@ def test_conv_weekly(self): assert ival_W.asfreq('W') == ival_W - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with tm.assert_raises_regex(ValueError, msg): ival_W.asfreq('WK') def test_conv_weekly_legacy(self): # frequency conversion tests: from Weekly Frequency - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with tm.assert_raises_regex(ValueError, msg): Period(freq='WK', year=2007, month=1, day=1) @@ -738,7 +738,7 @@ def test_asfreq_MS(self): assert initial.asfreq(freq="M", how="S") == Period('2013-01', 'M') - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with tm.assert_raises_regex(ValueError, msg): initial.asfreq(freq="MS", how="S") diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index ffc375ba12e34..eccd86a888fb9 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -515,7 +515,7 @@ def test_period_deprecated_freq(self): "U": ["MICROSECOND", "MICROSECONDLY", "microsecond"], "N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]} - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG for exp, freqs in iteritems(cases): for freq in freqs: with tm.assert_raises_regex(ValueError, msg): @@ -759,7 +759,7 @@ def test_properties_weekly_legacy(self): exp = Period(freq='W', year=2012, month=2, day=1) assert exp.days_in_month == 29 - msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR + msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG with tm.assert_raises_regex(ValueError, msg): Period(freq='WK', year=2007, month=1, day=7) diff --git a/pandas/tests/scalar/timestamp/test_unary_ops.py b/pandas/tests/scalar/timestamp/test_unary_ops.py index fef01512b2060..9f000a6f22cd6 100644 --- a/pandas/tests/scalar/timestamp/test_unary_ops.py +++ b/pandas/tests/scalar/timestamp/test_unary_ops.py @@ -11,7 +11,7 @@ from pandas.compat import PY3 from pandas._libs.tslibs import conversion -from pandas._libs.tslibs.frequencies import _INVALID_FREQ_ERROR +from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG from pandas import Timestamp, NaT @@ -82,7 +82,7 @@ def test_round_nonstandard_freq(self): def test_round_invalid_arg(self): stamp = Timestamp('2000-01-05 05:09:15.13') - with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR): + with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG): stamp.round('foo') @pytest.mark.parametrize('freq, expected', [ diff --git a/pandas/tests/tseries/offsets/test_fiscal.py b/pandas/tests/tseries/offsets/test_fiscal.py index c084cccbb74ac..ccd418a69c827 100644 --- a/pandas/tests/tseries/offsets/test_fiscal.py +++ b/pandas/tests/tseries/offsets/test_fiscal.py @@ -11,7 +11,7 @@ from pandas import Timestamp from pandas.tseries.frequencies import get_offset -from pandas._libs.tslibs.frequencies import _INVALID_FREQ_ERROR +from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG from pandas.tseries.offsets import FY5253Quarter, FY5253 from pandas._libs.tslibs.offsets import WeekDay @@ -45,9 +45,9 @@ def test_get_offset_name(): def test_get_offset(): - with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR): + with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG): get_offset('gibberish') - with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR): + with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG): get_offset('QS-JAN-B') pairs = [ diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index 79df847d0c8aa..0c5470e7bd932 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -12,7 +12,7 @@ from pandas.core.series import Series from pandas._libs.tslibs import conversion from pandas._libs.tslibs.frequencies import (get_freq_code, get_freq_str, - _INVALID_FREQ_ERROR) + INVALID_FREQ_ERR_MSG) from pandas.tseries.frequencies import _offset_map, get_offset from pandas.core.indexes.datetimes import ( _to_m8, DatetimeIndex, _daterange_cache) @@ -2748,9 +2748,9 @@ def test_get_offset_name(self): def test_get_offset(): - with pytest.raises(ValueError, match=_INVALID_FREQ_ERROR): + with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): get_offset('gibberish') - with pytest.raises(ValueError, match=_INVALID_FREQ_ERROR): + with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): get_offset('QS-JAN-B') pairs = [ @@ -2768,7 +2768,7 @@ def test_get_offset(): def test_get_offset_legacy(): pairs = [('w@Sat', Week(weekday=5))] for name, expected in pairs: - with pytest.raises(ValueError, match=_INVALID_FREQ_ERROR): + with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): get_offset(name) diff --git a/pandas/tests/tseries/test_frequencies.py b/pandas/tests/tseries/test_frequencies.py index 92d7eb15c929c..f90c8e449f92c 100644 --- a/pandas/tests/tseries/test_frequencies.py +++ b/pandas/tests/tseries/test_frequencies.py @@ -8,7 +8,7 @@ date_range, period_range) from pandas._libs.tslibs.frequencies import (_period_code_map, - _INVALID_FREQ_ERROR) + INVALID_FREQ_ERR_MSG) from pandas._libs.tslibs.ccalendar import MONTHS from pandas._libs.tslibs import resolution import pandas.tseries.frequencies as frequencies @@ -797,7 +797,7 @@ def test_legacy_offset_warnings(self): 'WOM@4THU', 'WOM@1FRI', 'WOM@2FRI', 'WOM@3FRI', 'WOM@4FRI'] - msg = _INVALID_FREQ_ERROR + msg = INVALID_FREQ_ERR_MSG for freq in freqs: with tm.assert_raises_regex(ValueError, msg): frequencies.get_offset(freq) diff --git a/pandas/tests/tslibs/test_libfrequencies.py b/pandas/tests/tslibs/test_libfrequencies.py index 601d542da3095..f4083dfb2bd1c 100644 --- a/pandas/tests/tslibs/test_libfrequencies.py +++ b/pandas/tests/tslibs/test_libfrequencies.py @@ -5,7 +5,7 @@ from pandas.tseries import offsets from pandas._libs.tslibs.frequencies import (get_rule_month, _period_str_to_code, - _INVALID_FREQ_ERROR, + INVALID_FREQ_ERR_MSG, is_superperiod, is_subperiod) @@ -14,7 +14,7 @@ def assert_aliases_deprecated(freq, expected, aliases): assert (_period_str_to_code(freq) == expected) for alias in aliases: - with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR): + with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG): _period_str_to_code(alias) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 0cffd818202ed..b161e7764b884 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -142,7 +142,7 @@ def to_offset(freq): else: delta = delta + offset except Exception: - raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(freq)) + raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq)) else: delta = None @@ -173,10 +173,10 @@ def to_offset(freq): else: delta = delta + offset except Exception: - raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(freq)) + raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq)) if delta is None: - raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(freq)) + raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq)) return delta @@ -205,7 +205,7 @@ def get_offset(name): offset = klass._from_name(*split[1:]) except (ValueError, TypeError, KeyError): # bad prefix or suffix - raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(name)) + raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(name)) # cache _offset_map[name] = offset # do not return cache because it's mutable