Skip to content

Commit 9cbf895

Browse files
reidy-pjreback
authored andcommitted
ERR: Raise ValueError when week is passed in to_datetime format witho… (#17819)
1 parent 8d16271 commit 9cbf895

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ Other API Changes
784784
- :func:`Series.argmin` and :func:`Series.argmax` will now raise a ``TypeError`` when used with ``object`` dtypes, instead of a ``ValueError`` (:issue:`13595`)
785785
- :class:`Period` is now immutable, and will now raise an ``AttributeError`` when a user tries to assign a new value to the ``ordinal`` or ``freq`` attributes (:issue:`17116`).
786786
- :func:`to_datetime` when passed a tz-aware ``origin=`` kwarg will now raise a more informative ``ValueError`` rather than a ``TypeError`` (:issue:`16842`)
787+
- :func:`to_datetime` now raises a ``ValueError`` when format includes ``%W`` or ``%U`` without also including day of the week and calendar year (:issue:`16774`)
787788
- Renamed non-functional ``index`` to ``index_col`` in :func:`read_stata` to improve API consistency (:issue:`16342`)
788789
- Bug in :func:`DataFrame.drop` caused boolean labels ``False`` and ``True`` to be treated as labels 0 and 1 respectively when dropping indices from a numeric index. This will now raise a ValueError (:issue:`16877`)
789790
- Restricted DateOffset keyword arguments. Previously, ``DateOffset`` subclasses allowed arbitrary keyword arguments which could lead to unexpected behavior. Now, only valid arguments will be accepted. (:issue:`17176`).

pandas/_libs/tslibs/strptime.pyx

+10
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ def array_strptime(ndarray[object] values, object fmt,
8383

8484
assert is_raise or is_ignore or is_coerce
8585

86+
if fmt is not None:
87+
if '%W' in fmt or '%U' in fmt:
88+
if '%Y' not in fmt and '%y' not in fmt:
89+
raise ValueError("Cannot use '%W' or '%U' without "
90+
"day and year")
91+
if ('%A' not in fmt and '%a' not in fmt and '%w' not
92+
in fmt):
93+
raise ValueError("Cannot use '%W' or '%U' without "
94+
"day and year")
95+
8696
global _TimeRE_cache, _regex_cache
8797
with _cache_lock:
8898
if _getlang() != _TimeRE_cache.locale_time.lang:

pandas/tests/indexes/datetimes/test_tools.py

+14
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,20 @@ def test_datetime_invalid_datatype(self):
372372
with pytest.raises(TypeError):
373373
pd.to_datetime(pd.to_datetime)
374374

375+
@pytest.mark.parametrize('date, format',
376+
[('2017-20', '%Y-%W'),
377+
('20 Sunday', '%W %A'),
378+
('20 Sun', '%W %a'),
379+
('2017-21', '%Y-%U'),
380+
('20 Sunday', '%U %A'),
381+
('20 Sun', '%U %a')])
382+
def test_week_without_day_and_calendar_year(self, date, format):
383+
# GH16774
384+
385+
msg = "Cannot use '%W' or '%U' without day and year"
386+
with tm.assert_raises_regex(ValueError, msg):
387+
pd.to_datetime(date, format=format)
388+
375389

376390
class TestToDatetimeUnit(object):
377391

0 commit comments

Comments
 (0)