Skip to content

Commit 1b03b66

Browse files
committed
ERR: Raise ValueError when week is passed in to_datetime format without day or year (pandas-dev#16774)
1 parent f9ba6fe commit 1b03b66

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ Other API Changes
701701
- :func:`Series.argmin` and :func:`Series.argmax` will now raise a ``TypeError`` when used with ``object`` dtypes, instead of a ``ValueError`` (:issue:`13595`)
702702
- :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`).
703703
- :func:`to_datetime` when passed a tz-aware ``origin=`` kwarg will now raise a more informative ``ValueError`` rather than a ``TypeError`` (:issue:`16842`)
704+
- :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`)
704705
- Renamed non-functional ``index`` to ``index_col`` in :func:`read_stata` to improve API consistency (:issue:`16342`)
705706
- 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`)
706707
- 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/core/tools/datetimes.py

+10
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,16 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
254254
require_iso8601 = not infer_datetime_format
255255
format = None
256256

257+
if format is not None:
258+
if '%W' in format or '%U' in format:
259+
if '%Y' not in format and '%y' not in format:
260+
raise ValueError("Cannot use '%W' or '%U' without "
261+
"day and year")
262+
if ('%A' not in format and '%a' not in format and '%w' not
263+
in format):
264+
raise ValueError("Cannot use '%W' or '%U' without "
265+
"day and year")
266+
257267
try:
258268
result = None
259269

pandas/tests/indexes/datetimes/test_tools.py

+13
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,19 @@ 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+
with pytest.raises(ValueError):
386+
pd.to_datetime(date, format=format)
387+
375388

376389
class TestToDatetimeUnit(object):
377390

0 commit comments

Comments
 (0)