Skip to content

Commit 4729766

Browse files
jbrockmendelTomAugspurger
authored andcommitted
BUG: wrong exception raised by Week+Day (#28530)
* BUG: Week+Day raising wrong exception * whatsnew
1 parent 3f8c0c4 commit 4729766

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Datetimelike
149149
- Bug in :class:`Timestamp` subtraction when subtracting a :class:`Timestamp` from a ``np.datetime64`` object incorrectly raising ``TypeError`` (:issue:`28286`)
150150
- Addition and subtraction of integer or integer-dtype arrays with :class:`Timestamp` will now raise ``NullFrequencyError`` instead of ``ValueError`` (:issue:`28268`)
151151
- Bug in :class:`Series` and :class:`DataFrame` with integer dtype failing to raise ``TypeError`` when adding or subtracting a ``np.datetime64`` object (:issue:`28080`)
152-
-
152+
- Bug in :class:`Week` with ``weekday`` incorrectly raising ``AttributeError`` instead of ``TypeError`` when adding or subtracting an invalid type (:issue:`28530`)
153153

154154

155155
Timedelta

pandas/tests/tseries/offsets/test_offsets.py

+9
Original file line numberDiff line numberDiff line change
@@ -4348,3 +4348,12 @@ def test_last_week_of_month_on_offset():
43484348
slow = (ts + offset) - offset == ts
43494349
fast = offset.onOffset(ts)
43504350
assert fast == slow
4351+
4352+
4353+
def test_week_add_invalid():
4354+
# Week with weekday should raise TypeError and _not_ AttributeError
4355+
# when adding invalid offset
4356+
offset = Week(weekday=1)
4357+
other = Day()
4358+
with pytest.raises(TypeError, match="Cannot add"):
4359+
offset + other

pandas/tseries/frequencies.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def to_offset(freq):
138138
delta = offset
139139
else:
140140
delta = delta + offset
141-
except Exception:
141+
except ValueError:
142142
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))
143143

144144
else:
@@ -170,7 +170,7 @@ def to_offset(freq):
170170
delta = offset
171171
else:
172172
delta = delta + offset
173-
except Exception:
173+
except (ValueError, TypeError):
174174
raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq))
175175

176176
if delta is None:

pandas/tseries/offsets.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def apply(self, other):
605605
return BDay(self.n, offset=self.offset + other, normalize=self.normalize)
606606
else:
607607
raise ApplyTypeError(
608-
"Only know how to combine business day with " "datetime or timedelta."
608+
"Only know how to combine business day with datetime or timedelta."
609609
)
610610

611611
@apply_index_wraps
@@ -1545,6 +1545,13 @@ def apply(self, other):
15451545
if self.weekday is None:
15461546
return other + self.n * self._inc
15471547

1548+
if not isinstance(other, datetime):
1549+
raise TypeError(
1550+
"Cannot add {typ} to {cls}".format(
1551+
typ=type(other).__name__, cls=type(self).__name__
1552+
)
1553+
)
1554+
15481555
k = self.n
15491556
otherDay = other.weekday()
15501557
if otherDay != self.weekday:

0 commit comments

Comments
 (0)