-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: standardize different freq message #24283
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6d5138
standardize different freq message
jbrockmendel a9b12ba
Merge branch 'master' of https://github.com/pandas-dev/pandas into pmsg
jbrockmendel 533ab47
implement raise_on_incompatible
jbrockmendel 51aec58
fixup missing import
jbrockmendel d46b494
Merge branch 'master' of https://github.com/pandas-dev/pandas into pmsg
jbrockmendel f43becf
freqstr cases
jbrockmendel c3074b8
update tested messages
jbrockmendel a72f6ce
Merge branch 'master' of https://github.com/pandas-dev/pandas into pmsg
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
from pandas._libs.tslibs import NaT, iNaT, period as libperiod | ||
from pandas._libs.tslibs.fields import isleapyear_arr | ||
from pandas._libs.tslibs.period import ( | ||
DIFFERENT_FREQ_INDEX, IncompatibleFrequency, Period, get_period_field_arr, | ||
DIFFERENT_FREQ, IncompatibleFrequency, Period, get_period_field_arr, | ||
period_asfreq_arr) | ||
from pandas._libs.tslibs.timedeltas import Timedelta, delta_to_nanoseconds | ||
import pandas.compat as compat | ||
|
@@ -30,7 +30,7 @@ | |
from pandas.core.missing import backfill_1d, pad_1d | ||
|
||
from pandas.tseries import frequencies | ||
from pandas.tseries.offsets import Tick | ||
from pandas.tseries.offsets import DateOffset, Tick, _delta_to_tick | ||
|
||
|
||
def _field_accessor(name, alias, docstring=None): | ||
|
@@ -166,8 +166,9 @@ def __init__(self, values, freq=None, dtype=None, copy=False): | |
|
||
if isinstance(values, type(self)): | ||
if freq is not None and freq != values.freq: | ||
msg = DIFFERENT_FREQ_INDEX.format(values.freq.freqstr, | ||
freq.freqstr) | ||
msg = DIFFERENT_FREQ.format(cls=type(self).__name__, | ||
own_freq=values.freq.freqstr, | ||
other_freq=freq.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
values, freq = values._data, values.freq | ||
|
||
|
@@ -239,8 +240,7 @@ def _generate_range(cls, start, end, periods, freq, fields): | |
|
||
def _check_compatible_with(self, other): | ||
if self.freqstr != other.freqstr: | ||
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
_raise_on_incompatible(self, other) | ||
|
||
# -------------------------------------------------------------------- | ||
# Data / Attributes | ||
|
@@ -372,15 +372,13 @@ def __setitem__( | |
value = period_array(value) | ||
|
||
if self.freqstr != value.freqstr: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, value.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
_raise_on_incompatible(self, value) | ||
|
||
value = value.asi8 | ||
elif isinstance(value, Period): | ||
|
||
if self.freqstr != value.freqstr: | ||
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, value.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
_raise_on_incompatible(self, value) | ||
|
||
value = value.ordinal | ||
elif isna(value): | ||
|
@@ -696,8 +694,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) | ||
raise IncompatibleFrequency(msg) | ||
_raise_on_incompatible(self, other) | ||
|
||
# Note: when calling parent class's _add_timedeltalike_scalar, | ||
# it will call delta_to_nanoseconds(delta). Because delta here | ||
|
@@ -760,10 +757,7 @@ def _add_delta(self, other): | |
""" | ||
if not isinstance(self.freq, Tick): | ||
# We cannot add timedelta-like to non-tick PeriodArray | ||
raise IncompatibleFrequency("Input has different freq from " | ||
"{cls}(freq={freqstr})" | ||
.format(cls=type(self).__name__, | ||
freqstr=self.freqstr)) | ||
_raise_on_incompatible(self, other) | ||
|
||
new_ordinals = super(PeriodArray, self)._add_delta(other) | ||
return type(self)(new_ordinals, freq=self.freq) | ||
|
@@ -815,10 +809,7 @@ def _check_timedeltalike_freq_compat(self, other): | |
# by which will be added to self. | ||
return delta | ||
|
||
raise IncompatibleFrequency("Input has different freq from " | ||
"{cls}(freq={freqstr})" | ||
.format(cls=type(self).__name__, | ||
freqstr=self.freqstr)) | ||
_raise_on_incompatible(self, other) | ||
|
||
def _values_for_argsort(self): | ||
return self._data | ||
|
@@ -827,6 +818,34 @@ def _values_for_argsort(self): | |
PeriodArray._add_comparison_ops() | ||
|
||
|
||
def _raise_on_incompatible(left, right): | ||
""" | ||
Helper function to render a consistent error message when raising | ||
IncompatibleFrequency. | ||
|
||
Parameters | ||
---------- | ||
left : PeriodArray | ||
right : DateOffset, Period, ndarray, or timedelta-like | ||
|
||
Raises | ||
------ | ||
IncompatibleFrequency | ||
""" | ||
# GH#24283 error message format depends on whether right is scalar | ||
if isinstance(right, np.ndarray): | ||
other_freq = None | ||
elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period, DateOffset)): | ||
other_freq = right.freqstr | ||
else: | ||
other_freq = _delta_to_tick(Timedelta(right)).freqstr | ||
|
||
msg = DIFFERENT_FREQ.format(cls=type(left).__name__, | ||
own_freq=left.freqstr, | ||
other_freq=other_freq) | ||
raise IncompatibleFrequency(msg) | ||
|
||
|
||
# ------------------------------------------------------------------- | ||
# Constructor Helpers | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.