Skip to content
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

ERR: Raise ValueError when week is passed in to_datetime format witho… #17819

Merged
merged 3 commits into from
Oct 14, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ Other API Changes
- :func:`Series.argmin` and :func:`Series.argmax` will now raise a ``TypeError`` when used with ``object`` dtypes, instead of a ``ValueError`` (:issue:`13595`)
- :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`).
- :func:`to_datetime` when passed a tz-aware ``origin=`` kwarg will now raise a more informative ``ValueError`` rather than a ``TypeError`` (:issue:`16842`)
- :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`)
- Renamed non-functional ``index`` to ``index_col`` in :func:`read_stata` to improve API consistency (:issue:`16342`)
- 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`)
- 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`).
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
require_iso8601 = not infer_datetime_format
format = None

if format is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this check to

pandas/_libs/tslibs/strptime.pyx

its just closer to the code where we use it

if '%W' in format or '%U' in format:
if '%Y' not in format and '%y' not in format:
raise ValueError("Cannot use '%W' or '%U' without "
"day and year")
if ('%A' not in format and '%a' not in format and '%w' not
in format):
raise ValueError("Cannot use '%W' or '%U' without "
"day and year")

try:
result = None

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,20 @@ def test_datetime_invalid_datatype(self):
with pytest.raises(TypeError):
pd.to_datetime(pd.to_datetime)

@pytest.mark.parametrize('date, format',
[('2017-20', '%Y-%W'),
('20 Sunday', '%W %A'),
('20 Sun', '%W %a'),
('2017-21', '%Y-%U'),
('20 Sunday', '%U %A'),
('20 Sun', '%U %a')])
def test_week_without_day_and_calendar_year(self, date, format):
# GH16774

msg = "Cannot use '%W' or '%U' without day and year"
with tm.assert_raises_regex(ValueError, msg):
pd.to_datetime(date, format=format)


class TestToDatetimeUnit(object):

Expand Down