Skip to content

BUG: wrong exception raised by Week+Day #28530

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 9 commits into from
Sep 19, 2019
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Datetimelike
- Bug in :class:`Timestamp` subtraction when subtracting a :class:`Timestamp` from a ``np.datetime64`` object incorrectly raising ``TypeError`` (:issue:`28286`)
- Addition and subtraction of integer or integer-dtype arrays with :class:`Timestamp` will now raise ``NullFrequencyError`` instead of ``ValueError`` (:issue:`28268`)
- Bug in :class:`Series` and :class:`DataFrame` with integer dtype failing to raise ``TypeError`` when adding or subtracting a ``np.datetime64`` object (:issue:`28080`)
-
- Bug in :class:`Week` with ``weekday`` incorrectly raising ``AttributeError`` instead of ``TypeError`` when adding or subtracting an invalid type (:issue:`28530`)


Timedelta
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4348,3 +4348,12 @@ def test_last_week_of_month_on_offset():
slow = (ts + offset) - offset == ts
fast = offset.onOffset(ts)
assert fast == slow


def test_week_add_invalid():
# Week with weekday should raise TypeError and _not_ AttributeError
# when adding invalid offset
offset = Week(weekday=1)
other = Day()
with pytest.raises(TypeError, match="Cannot add"):
offset + other
4 changes: 2 additions & 2 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def to_offset(freq):
delta = offset
else:
delta = delta + offset
except Exception:
except ValueError:
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))

else:
Expand Down Expand Up @@ -170,7 +170,7 @@ def to_offset(freq):
delta = offset
else:
delta = delta + offset
except Exception:
except (ValueError, TypeError):
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))

if delta is None:
Expand Down
9 changes: 8 additions & 1 deletion pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def apply(self, other):
return BDay(self.n, offset=self.offset + other, normalize=self.normalize)
else:
raise ApplyTypeError(
"Only know how to combine business day with " "datetime or timedelta."
"Only know how to combine business day with datetime or timedelta."
)

@apply_index_wraps
Expand Down Expand Up @@ -1545,6 +1545,13 @@ def apply(self, other):
if self.weekday is None:
return other + self.n * self._inc

if not isinstance(other, datetime):
raise TypeError(
"Cannot add {typ} to {cls}".format(
typ=type(other).__name__, cls=type(self).__name__
)
)

k = self.n
otherDay = other.weekday()
if otherDay != self.weekday:
Expand Down