to_datetime parses dates incorrectly when the format includes '%W' and does not include day of week plus calendar year · Issue #16774 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

import pandas as pd

I'm not sure how a bare week should be interpreted. But the result should

probably still be of the same week number

print(pd.to_datetime('20', format='%W').strftime('%W')) # 01

Parsing is broken even when the week is fully specified

print(pd.to_datetime('2017-20', format='%Y-%W').strftime('%Y-%W')) # 2017-00

Problem description

to_datetime parses a week XX as 0 for any valid XX. Interestingly, an impossible XX (ex: '70') throws a ValueError as expected.

Expected Output

20
2017-20

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.4.3.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-79-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 36.0.1
Cython: 0.24.1
numpy: 1.13.0
scipy: 0.18.1
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: 2.4.0
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.3.3
bs4: 4.2.1
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None