Skip to content

DOC: how to_datetime %S differs from strptime #47134

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

Closed
1 task done
mdavis-xyz opened this issue May 27, 2022 · 8 comments · Fixed by #48376
Closed
1 task done

DOC: how to_datetime %S differs from strptime #47134

mdavis-xyz opened this issue May 27, 2022 · 8 comments · Fixed by #48376

Comments

@mdavis-xyz
Copy link
Contributor

mdavis-xyz commented May 27, 2022

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.

@mdavis-xyz mdavis-xyz added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels May 27, 2022
@MaruthiKo
Copy link
Contributor

Hi, I would to like work on this issue.

@MarcoGorelli MarcoGorelli added good first issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 17, 2022
@MarcoGorelli
Copy link
Member

Thanks for the suggestion, sounds good - as noted in https://github.com/pandas-dev/pandas/pull/47164/files#r884317700 , it would be good to include an example

@WooilKim
Copy link

Let me add an example on this.

@WooilKim
Copy link

WooilKim commented Aug 23, 2022

@MarcoGorelli I think @mdavis-xyz required to revise the html document, not the pandas/core/tools/datetimes.py.
Is it possible to modify that html document?
By the way, if the documentation of datetimes.py also needs revision, I'll do it including the example.
Thank you.

@ryuusama09
Copy link

Is this issue still open ?. If yes then I would like to work on it

@yadomkar
Copy link
Contributor

yadomkar commented Sep 2, 2022

I am interested in working on this issue.

@mdavis-xyz
Copy link
Contributor Author

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

yadomkar added a commit to yadomkar/pandas that referenced this issue Sep 3, 2022
gfyoung pushed a commit that referenced this issue Sep 3, 2022
* doc update for #47134

* black and trailing spaces fixed

* rewording_1 #48376

Co-authored-by: Marco Edward Gorelli <[email protected]>

* rewording_2 #48376

Co-authored-by: Marco Edward Gorelli <[email protected]>

Co-authored-by: Marco Edward Gorelli <[email protected]>
@MarcoGorelli
Copy link
Member

Part of this needs reverting, #49231

noatamir pushed a commit to noatamir/pandas that referenced this issue Nov 9, 2022
* doc update for pandas-dev#47134

* black and trailing spaces fixed

* rewording_1 pandas-dev#48376

Co-authored-by: Marco Edward Gorelli <[email protected]>

* rewording_2 pandas-dev#48376

Co-authored-by: Marco Edward Gorelli <[email protected]>

Co-authored-by: Marco Edward Gorelli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants