Skip to content

BUG: %p not working in pd.to_datetime #46038

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
2 of 3 tasks
JulianWgs opened this issue Feb 17, 2022 · 4 comments
Closed
2 of 3 tasks

BUG: %p not working in pd.to_datetime #46038

JulianWgs opened this issue Feb 17, 2022 · 4 comments
Labels
Closing Candidate May be closeable, needs more eyeballs datetime.date stdlib datetime.date support Datetime Datetime data dtype Needs Triage Issue that has not been reviewed by a pandas team member Usage Question

Comments

@JulianWgs
Copy link

JulianWgs commented Feb 17, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
pd.to_datetime("2/17/2022 4:16:51 PM", format="%m/%d/%Y %H:%M:%S %p")

returns

Timestamp('2022-02-17 04:16:51')

but should return

Timestamp('2022-02-17 16:16:51')

Issue Description

%p format string is ignored, when using to_datetime.It is a valid format string according to https://strftime.org/ and the pandas docs and doesn't raise an error.

Expected Behavior

The format is either parsed correctly or an error is returned that %p is not supported.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-99-generic
Version : #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.1
numpy : 1.20.3
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 50.3.2
Cython : 0.29.26
pytest : 6.2.2
hypothesis : 6.8.1
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.7.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.30.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fastparquet : None
fsspec : 2021.11.1
gcsfs : None
matplotlib : 3.5.0
numba : 0.54.1
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 4.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : None
tabulate : 0.8.7
xarray : 0.19.0
xlrd : 2.0.1
xlwt : None
zstandard : None

@JulianWgs JulianWgs added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 17, 2022
@JulianWgs JulianWgs changed the title BUG: BUG: %p not working in pd.to_datetime Feb 17, 2022
@mroeschke
Copy link
Member

This seems to match the stdlib behavior

In [1]: import datetime

In [2]: datetime.datetime.strptime("2/17/2022 4:16:51 PM", "%m/%d/%Y %H:%M:%S %p")
Out[2]: datetime.datetime(2022, 2, 17, 4, 16, 51)

@phofl phofl added Closing Candidate May be closeable, needs more eyeballs Datetime Datetime data dtype datetime.date stdlib datetime.date support labels Feb 18, 2022
@JeremyVriens
Copy link

JeremyVriens commented Feb 18, 2022

This is expected behaviour. When you look at the Python documentation: https://docs.python.org/3/library/time.html#time.strftime it has a note on %p that says:

When used with the strptime() function, the %p directive only affects the output hour field if the %I directive is used to parse the hour.

So the correct format should be %m/%d/%Y %I:%M:%S %p (note the %H got replaced with %I, capital i) which results in:

In [1]: import datetime

In [2]: datetime.datetime.strptime("2/17/2022 4:16:51 PM", "%m/%d/%Y %I:%M:%S %p")
Out[2]: datetime.datetime(2022, 2, 17, 16, 16, 51)

I do agree that maybe Python should raise a warning when %p is used without %I in the format, however this is a discussion on Python level, not on Pandas.

@mroeschke
Copy link
Member

Thanks closing as a usage question

@phofl phofl added this to the No action milestone Feb 18, 2022
@JulianWgs
Copy link
Author

Sorry, for the inconvenience :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs datetime.date stdlib datetime.date support Datetime Datetime data dtype Needs Triage Issue that has not been reviewed by a pandas team member Usage Question
Projects
None yet
Development

No branches or pull requests

4 participants