Skip to content

BUG: to_datetime ignores "AM" and "PM" unless format is explicitly set #53147

Closed
@juli4nb4dillo

Description

@juli4nb4dillo

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
>>> df = pd.DataFrame(['4/21/2023 11:06:35 AM','4/21/2023 12:06:35 PM', '4/21/2023 1:00:48 PM', '4/21/2023 2:35:37 PM'])
>>> df
                       0
0  4/21/2023 11:06:35 AM
1  4/21/2023 12:06:35 PM
2   4/21/2023 1:00:48 PM
3   4/21/2023 2:35:37 PM
>>> pd.to_datetime(df[0])
0   2023-04-21 11:06:35
1   2023-04-21 12:06:35
2   2023-04-21 01:00:48  # PM is ignored, parsing as 01:00 instead of 13:00
3   2023-04-21 02:35:37

Issue Description

to_datetime ignores the AM and PM particles, hence parsing both as 24H format.

Expected Behavior

>>> df = pd.DataFrame(['4/21/2023 11:06:35 AM','4/21/2023 12:06:35 PM', '4/21/2023 1:00:48 PM', '4/21/2023 2:35:37 PM'])
>>> df
                       0
0  4/21/2023 11:06:35 AM
1  4/21/2023 12:06:35 PM
2   4/21/2023 1:00:48 PM
3   4/21/2023 2:35:37 PM
>>> pd.to_datetime(df[0])
0   2023-04-21 11:06:35
1   2023-04-21 12:06:35
2   2023-04-21 13:00:48
3   2023-04-21 14:35:37

Installed Versions

INSTALLED VERSIONS

commit : 37ea63d
python : 3.10.4.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Thu Mar 9 20:12:21 PST 2023; root:xnu-8020.240.18.700.8~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.1
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.4.0
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.9
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : 2.9.5
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions