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.

DOC: how to_datetime %S differs from strptime

See original GitHub issue

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/reference/api/pandas.to_datetime.html?highlight=to_datetime#pandas.to_datetime

Documentation problem

The documentation says that the formatting strings used by pandas.to_datetime are the same as datetime.strptime, other than the number of digits %f handles.

This is not quite true, there is another difference. pandas.to_datetime with %S (without %f) will capture microseconds. strptime will throw an error. (Which makes pandas.to_datetime more useful than strptime)

import pandas as pd
import datetime as dt
fmt = "%Y/%m/%d %H:%M:%S"
s= "2021/01/01 12:01:01.123"
assert pd.to_datetime(s, format=fmt).microsecond > 0
dt.datetime.strptime(s, fmt)

The second-last line passes, the last line throws ValueError: unconverted data remains: .123.

Suggested fix for documentation

Change:

Note that “%f” will parse all the way up to nanoseconds.

to:

Note that “%S” without “%f” will capture fractional seconds up to nanoseconds, Note that “%f” will parse all the way up to nanoseconds. “%S” without “%f” will capture all the way up to nanoseconds if present as decimal places, but will also handle the case where the number of seconds is an integer.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mdavis-xyzcommented, Sep 2, 2022

Yes I think this issue is still open. That PR was closed as stale. I think the reviewers wanted examples included.

0reactions
MarcoGorellicommented, Oct 21, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python strptime() - string to datetime object - Programiz
The strptime() method creates a datetime object from the given string. Note: You cannot create datetime object from every string.
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is...
Read more >
How To Convert a String to a datetime or time Object in Python
The datetime.strptime() method returns a datetime object that matches the date_string parsed by the format. Both arguments are required and must ...
Read more >
Difference between two different dates in python
The difference between 2 datetime.datetime objects is represented with datetime.timedelta . From the official documentation:
Read more >
strptime() — Convert String to Date/Time - IBM
On successful completion, the strptime() function returns a pointer to the character following the last character parsed. Otherwise, a null pointer is returned....
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