Skip to content

De-privatize tslibs functions #21714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/frequencies.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 9 additions & 11 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/period/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/timedeltas/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/scalar/period/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/scalar/timestamp/test_unary_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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', [
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/tseries/offsets/test_fiscal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = [
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = [
Expand All @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tseries/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tslibs/test_libfrequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)


Expand Down
8 changes: 4 additions & 4 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down