question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

to_datetime function not working with %Y.%m.%d %H format

See original GitHub issue

Code Sample, a copy-pastable example if possible

import pandas as pd
import datetime as dt
print(dt.datetime.strptime('2012.01.01 1', '%Y.%m.%d %H')) 
# -> 2012-01-01 01:00:00
print(pd.to_datetime('2012.01.01 1', format='%Y.%m.%d %H')) 
# -> ValueError: time data '2012.01.01 1' doesn't match format specified

Problem description

When using to_datetime to parse a date that only includes an hour component, but not minutes and seconds, with a format that is otherwise similar to ISO8601 (such as the format '%Y.%m.%d %H'), a ValueError is raised (see above). This behavior is unexpected as strptime can parse the same date without any problem, using the same format string (see above).

I suspect that the problem is in the _format_is_iso function of pandas._libs.tslibs.parsing, where it is just checked if the ISO format starts with the format given - so this format is recognized as being ISO-like. In this case, the format passed to to_datetime is ignored and tslib.array_to_datetime function is used to parse the date instead, which doesn’t seem to be able to handle this kind of format.

My current workaround is to modify the dates to also have a minutes component (append ':00' to every string) so that they can be parsed.

Expected Output

2012-01-01 01:00:00 (same as when using strptime)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.5.3.final.0 python-bits: 64 OS: Linux OS-release: 4.9.0-6-amd64 machine: x86_64 processor: byteorder: little LC_ALL: None LANG: de_DE.UTF-8 LOCALE: de_DE.UTF-8

pandas: 0.22.0 pytest: None pip: 9.0.1 setuptools: 38.5.2 Cython: None numpy: 1.14.2 scipy: 0.19.0 pyarrow: None xarray: None IPython: None sphinx: None patsy: None dateutil: 2.7.2 pytz: 2018.3 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 2.2.0 openpyxl: 2.5.0 xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: None sqlalchemy: None pymysql: None psycopg2: None jinja2: None s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
chris-b1commented, Jun 14, 2018

To me it’s only a question of whether we consider "2016.05.06 1" an ISO 8601 (loosely as we use it) formatted string, i.e. parses even if no format is passed.

Even if the answer to that is no, we need to match python if an explicit format spec is passed, so some change is necessary either way. It’s a bug for pd.to_datetime(..., format='format') not to match datetime.strptime(..., 'format')

1reaction
MarcoGorellicommented, Dec 17, 2022

This’ll be addressed in https://github.com/pandas-dev/pandas/pull/50242

thanks for the report!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple PHP time format not working! - Stack Overflow
To display the dates in the format you want, use echo $dt->format('Y-m-d H:i:s'); // for DateTime echo date('Y-m-d H:i:s', $time); // for ...
Read more >
DateTimeInterface::format - Manual - PHP
format character Description Example returned values Day ‑‑‑ ‑‑‑ d Day of the month, 2 digits with leading zeros 01 to 31 D A textual representation...
Read more >
Formatting the Current Date and Time in PHP - Code
Getting the Date and Time in String Format. date($format, $timestamp) is one of the most commonly used date and time functions available in...
Read more >
How to change Excel date format and create custom formatting
Use the DATEVALUE() function to convert an Excel date to a serial number, ... Excel date format not working - fixes and solutions....
Read more >
9 Working with dates | The Epidemiologist R Handbook
Date() is not telling R the format you want the dates to be, ... The ymd() function flexibly converts date values supplied as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found