Skip to content

Commit 7b63769

Browse files
jbrockmendeljreback
authored andcommitted
De-privatize tslibs functions (#21714)
1 parent 2f14faf commit 7b63769

File tree

16 files changed

+41
-44
lines changed

16 files changed

+41
-44
lines changed

pandas/_libs/tslibs/frequencies.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ opattern = re.compile(
2020
r'([+\-]?\d*|[+\-]?\d*\.\d*)\s*([A-Za-z]+([\-][\dA-Za-z\-]+)?)'
2121
)
2222

23-
_INVALID_FREQ_ERROR = "Invalid frequency: {0}"
23+
INVALID_FREQ_ERR_MSG = "Invalid frequency: {0}"
2424

2525
# ---------------------------------------------------------------------
2626
# Period codes
@@ -220,7 +220,7 @@ cpdef _period_str_to_code(freqstr):
220220
try:
221221
return _period_code_map[freqstr]
222222
except KeyError:
223-
raise ValueError(_INVALID_FREQ_ERROR.format(freqstr))
223+
raise ValueError(INVALID_FREQ_ERR_MSG.format(freqstr))
224224

225225

226226
cpdef str get_freq_str(base, mult=1):

pandas/_libs/tslibs/period.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ def extract_ordinals(ndarray[object] values, freq):
895895
ordinals[i] = p.ordinal
896896

897897
if p.freqstr != freqstr:
898-
msg = _DIFFERENT_FREQ_INDEX.format(freqstr, p.freqstr)
898+
msg = DIFFERENT_FREQ_INDEX.format(freqstr, p.freqstr)
899899
raise IncompatibleFrequency(msg)
900900

901901
except AttributeError:
@@ -985,8 +985,8 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
985985

986986

987987
_DIFFERENT_FREQ = "Input has different freq={1} from Period(freq={0})"
988-
_DIFFERENT_FREQ_INDEX = ("Input has different freq={1} "
989-
"from PeriodIndex(freq={0})")
988+
DIFFERENT_FREQ_INDEX = ("Input has different freq={1} "
989+
"from PeriodIndex(freq={0})")
990990

991991

992992
class IncompatibleFrequency(ValueError):

pandas/core/indexes/accessors.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from pandas.core.accessor import PandasDelegate
1616
from pandas.core.base import NoNewAttributesMixin, PandasObject
1717
from pandas.core.indexes.datetimes import DatetimeIndex
18-
from pandas._libs.tslibs.period import IncompatibleFrequency # noqa
1918
from pandas.core.indexes.period import PeriodIndex
2019
from pandas.core.indexes.timedeltas import TimedeltaIndex
2120
from pandas.core.algorithms import take_1d

pandas/core/indexes/datetimelike.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pandas._libs import lib, iNaT, NaT, Timedelta
1616
from pandas._libs.tslibs.period import (Period, IncompatibleFrequency,
17-
_DIFFERENT_FREQ_INDEX)
17+
DIFFERENT_FREQ_INDEX)
1818
from pandas._libs.tslibs.timestamps import round_ns
1919

2020
from pandas.core.dtypes.common import (
@@ -732,7 +732,7 @@ def _sub_period_array(self, other):
732732
if not len(self) == len(other):
733733
raise ValueError("cannot subtract indices of unequal length")
734734
if self.freq != other.freq:
735-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
735+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
736736
raise IncompatibleFrequency(msg)
737737

738738
new_values = checked_add_with_arr(self.asi8, -other.asi8,

pandas/core/indexes/period.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas._libs import tslib, index as libindex
3131
from pandas._libs.tslibs.period import (Period, IncompatibleFrequency,
3232
get_period_field_arr,
33+
DIFFERENT_FREQ_INDEX,
3334
_validate_end_alias, _quarter_to_myear)
3435
from pandas._libs.tslibs.fields import isleapyear_arr
3536
from pandas._libs.tslibs import resolution, period
@@ -71,9 +72,6 @@ def dt64arr_to_periodarr(data, freq, tz):
7172
# --- Period index sketch
7273

7374

74-
_DIFFERENT_FREQ_INDEX = period._DIFFERENT_FREQ_INDEX
75-
76-
7775
def _period_index_cmp(opname, cls):
7876
"""
7977
Wrap comparison operations to convert Period-like to PeriodDtype
@@ -84,13 +82,13 @@ def wrapper(self, other):
8482
op = getattr(self._ndarray_values, opname)
8583
if isinstance(other, Period):
8684
if other.freq != self.freq:
87-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
85+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
8886
raise IncompatibleFrequency(msg)
8987

9088
result = op(other.ordinal)
9189
elif isinstance(other, PeriodIndex):
9290
if other.freq != self.freq:
93-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
91+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
9492
raise IncompatibleFrequency(msg)
9593

9694
result = op(other._ndarray_values)
@@ -523,7 +521,7 @@ def astype(self, dtype, copy=True, how='start'):
523521
def searchsorted(self, value, side='left', sorter=None):
524522
if isinstance(value, Period):
525523
if value.freq != self.freq:
526-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, value.freqstr)
524+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, value.freqstr)
527525
raise IncompatibleFrequency(msg)
528526
value = value.ordinal
529527
elif isinstance(value, compat.string_types):
@@ -706,7 +704,7 @@ def _maybe_convert_timedelta(self, other):
706704
base = frequencies.get_base_alias(freqstr)
707705
if base == self.freq.rule_code:
708706
return other.n
709-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
707+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
710708
raise IncompatibleFrequency(msg)
711709
elif is_integer(other):
712710
# integer is passed to .shift via
@@ -721,7 +719,7 @@ def _add_offset(self, other):
721719
assert not isinstance(other, Tick)
722720
base = frequencies.get_base_alias(other.rule_code)
723721
if base != self.freq.rule_code:
724-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
722+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
725723
raise IncompatibleFrequency(msg)
726724
return self.shift(other.n)
727725

@@ -753,7 +751,7 @@ def _sub_period(self, other):
753751
# If the operation is well-defined, we return an object-Index
754752
# of DateOffsets. Null entries are filled with pd.NaT
755753
if self.freq != other.freq:
756-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
754+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
757755
raise IncompatibleFrequency(msg)
758756

759757
asi8 = self.asi8
@@ -837,7 +835,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
837835
target = _ensure_index(target)
838836

839837
if hasattr(target, 'freq') and target.freq != self.freq:
840-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, target.freqstr)
838+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, target.freqstr)
841839
raise IncompatibleFrequency(msg)
842840

843841
if isinstance(target, PeriodIndex):
@@ -1010,7 +1008,7 @@ def _assert_can_do_setop(self, other):
10101008
raise ValueError('can only call with other PeriodIndex-ed objects')
10111009

10121010
if self.freq != other.freq:
1013-
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
1011+
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
10141012
raise IncompatibleFrequency(msg)
10151013

10161014
def _wrap_union_result(self, other, result):

pandas/tests/indexes/datetimes/test_scalar_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_round(self, tz):
101101
tm.assert_index_equal(rng.round(freq='H'), expected_rng)
102102
assert elt.round(freq='H') == expected_elt
103103

104-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
104+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
105105
with tm.assert_raises_regex(ValueError, msg):
106106
rng.round(freq='foo')
107107
with tm.assert_raises_regex(ValueError, msg):

pandas/tests/indexes/period/test_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def test_to_period_monthish(self):
167167
prng = rng.to_period()
168168
assert prng.freq == 'M'
169169

170-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
170+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
171171
with tm.assert_raises_regex(ValueError, msg):
172172
date_range('01-Jan-2012', periods=8, freq='EOM')
173173

pandas/tests/indexes/timedeltas/test_scalar_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_tdi_round(self):
5050
tm.assert_index_equal(td.round(freq='H'), expected_rng)
5151
assert elt.round(freq='H') == expected_elt
5252

53-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
53+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
5454
with tm.assert_raises_regex(ValueError, msg):
5555
td.round(freq='foo')
5656
with tm.assert_raises_regex(ValueError, msg):

pandas/tests/scalar/period/test_asfreq.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,13 @@ def test_conv_weekly(self):
325325

326326
assert ival_W.asfreq('W') == ival_W
327327

328-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
328+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
329329
with tm.assert_raises_regex(ValueError, msg):
330330
ival_W.asfreq('WK')
331331

332332
def test_conv_weekly_legacy(self):
333333
# frequency conversion tests: from Weekly Frequency
334-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
334+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
335335
with tm.assert_raises_regex(ValueError, msg):
336336
Period(freq='WK', year=2007, month=1, day=1)
337337

@@ -738,7 +738,7 @@ def test_asfreq_MS(self):
738738

739739
assert initial.asfreq(freq="M", how="S") == Period('2013-01', 'M')
740740

741-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
741+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
742742
with tm.assert_raises_regex(ValueError, msg):
743743
initial.asfreq(freq="MS", how="S")
744744

pandas/tests/scalar/period/test_period.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def test_period_deprecated_freq(self):
515515
"U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
516516
"N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"]}
517517

518-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
518+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
519519
for exp, freqs in iteritems(cases):
520520
for freq in freqs:
521521
with tm.assert_raises_regex(ValueError, msg):
@@ -759,7 +759,7 @@ def test_properties_weekly_legacy(self):
759759
exp = Period(freq='W', year=2012, month=2, day=1)
760760
assert exp.days_in_month == 29
761761

762-
msg = pd._libs.tslibs.frequencies._INVALID_FREQ_ERROR
762+
msg = pd._libs.tslibs.frequencies.INVALID_FREQ_ERR_MSG
763763
with tm.assert_raises_regex(ValueError, msg):
764764
Period(freq='WK', year=2007, month=1, day=7)
765765

pandas/tests/scalar/timestamp/test_unary_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from pandas.compat import PY3
1313
from pandas._libs.tslibs import conversion
14-
from pandas._libs.tslibs.frequencies import _INVALID_FREQ_ERROR
14+
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
1515
from pandas import Timestamp, NaT
1616

1717

@@ -82,7 +82,7 @@ def test_round_nonstandard_freq(self):
8282

8383
def test_round_invalid_arg(self):
8484
stamp = Timestamp('2000-01-05 05:09:15.13')
85-
with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR):
85+
with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG):
8686
stamp.round('foo')
8787

8888
@pytest.mark.parametrize('freq, expected', [

pandas/tests/tseries/offsets/test_fiscal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from pandas import Timestamp
1313
from pandas.tseries.frequencies import get_offset
14-
from pandas._libs.tslibs.frequencies import _INVALID_FREQ_ERROR
14+
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
1515
from pandas.tseries.offsets import FY5253Quarter, FY5253
1616
from pandas._libs.tslibs.offsets import WeekDay
1717

@@ -45,9 +45,9 @@ def test_get_offset_name():
4545

4646

4747
def test_get_offset():
48-
with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR):
48+
with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG):
4949
get_offset('gibberish')
50-
with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR):
50+
with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG):
5151
get_offset('QS-JAN-B')
5252

5353
pairs = [

pandas/tests/tseries/offsets/test_offsets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pandas.core.series import Series
1313
from pandas._libs.tslibs import conversion
1414
from pandas._libs.tslibs.frequencies import (get_freq_code, get_freq_str,
15-
_INVALID_FREQ_ERROR)
15+
INVALID_FREQ_ERR_MSG)
1616
from pandas.tseries.frequencies import _offset_map, get_offset
1717
from pandas.core.indexes.datetimes import (
1818
_to_m8, DatetimeIndex, _daterange_cache)
@@ -2748,9 +2748,9 @@ def test_get_offset_name(self):
27482748

27492749

27502750
def test_get_offset():
2751-
with pytest.raises(ValueError, match=_INVALID_FREQ_ERROR):
2751+
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
27522752
get_offset('gibberish')
2753-
with pytest.raises(ValueError, match=_INVALID_FREQ_ERROR):
2753+
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
27542754
get_offset('QS-JAN-B')
27552755

27562756
pairs = [
@@ -2768,7 +2768,7 @@ def test_get_offset():
27682768
def test_get_offset_legacy():
27692769
pairs = [('w@Sat', Week(weekday=5))]
27702770
for name, expected in pairs:
2771-
with pytest.raises(ValueError, match=_INVALID_FREQ_ERROR):
2771+
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
27722772
get_offset(name)
27732773

27742774

pandas/tests/tseries/test_frequencies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
date_range, period_range)
99

1010
from pandas._libs.tslibs.frequencies import (_period_code_map,
11-
_INVALID_FREQ_ERROR)
11+
INVALID_FREQ_ERR_MSG)
1212
from pandas._libs.tslibs.ccalendar import MONTHS
1313
from pandas._libs.tslibs import resolution
1414
import pandas.tseries.frequencies as frequencies
@@ -797,7 +797,7 @@ def test_legacy_offset_warnings(self):
797797
'WOM@4THU', 'WOM@1FRI', 'WOM@2FRI', 'WOM@3FRI',
798798
'WOM@4FRI']
799799

800-
msg = _INVALID_FREQ_ERROR
800+
msg = INVALID_FREQ_ERR_MSG
801801
for freq in freqs:
802802
with tm.assert_raises_regex(ValueError, msg):
803803
frequencies.get_offset(freq)

pandas/tests/tslibs/test_libfrequencies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pandas.tseries import offsets
66
from pandas._libs.tslibs.frequencies import (get_rule_month,
77
_period_str_to_code,
8-
_INVALID_FREQ_ERROR,
8+
INVALID_FREQ_ERR_MSG,
99
is_superperiod, is_subperiod)
1010

1111

@@ -14,7 +14,7 @@ def assert_aliases_deprecated(freq, expected, aliases):
1414
assert (_period_str_to_code(freq) == expected)
1515

1616
for alias in aliases:
17-
with tm.assert_raises_regex(ValueError, _INVALID_FREQ_ERROR):
17+
with tm.assert_raises_regex(ValueError, INVALID_FREQ_ERR_MSG):
1818
_period_str_to_code(alias)
1919

2020

pandas/tseries/frequencies.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def to_offset(freq):
142142
else:
143143
delta = delta + offset
144144
except Exception:
145-
raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(freq))
145+
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))
146146

147147
else:
148148
delta = None
@@ -173,10 +173,10 @@ def to_offset(freq):
173173
else:
174174
delta = delta + offset
175175
except Exception:
176-
raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(freq))
176+
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))
177177

178178
if delta is None:
179-
raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(freq))
179+
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))
180180

181181
return delta
182182

@@ -205,7 +205,7 @@ def get_offset(name):
205205
offset = klass._from_name(*split[1:])
206206
except (ValueError, TypeError, KeyError):
207207
# bad prefix or suffix
208-
raise ValueError(libfreqs._INVALID_FREQ_ERROR.format(name))
208+
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(name))
209209
# cache
210210
_offset_map[name] = offset
211211
# do not return cache because it's mutable

0 commit comments

Comments
 (0)