Skip to content

BUG: to_datetime() returns object instead of datetime type or raising exception #42229

Closed
@vlsd

Description

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

print(pd.to_datetime(['2021-03-14 12:05:45 -0400', '2021-03-14 03:06:00 -0400']))
print(pd.to_datetime(['2021-03-14 01:06:30 -0500', '2021-03-13 23:23:40 -0500']))
print(pd.to_datetime(['2021-03-14 03:06:00 -0400', '2021-03-14 01:06:30 -0500'], errors='raise'))
print(pd.to_datetime(['2021-03-14 03:06:00 -0400', '2021-03-14 01:06:30 -0500'], errors='coerce'))
print(pd.to_datetime(['2021-03-14 03:06:00 -0400', '2021-03-14 01:06:30 -0500'], errors='ignore'))

The output is

DatetimeIndex(['2021-03-14 12:05:45-04:00', '2021-03-14 03:06:00-04:00'], dtype='datetime64[ns, pytz.FixedOffset(-240)]', freq=None)
DatetimeIndex(['2021-03-14 01:06:30-05:00', '2021-03-13 23:23:40-05:00'], dtype='datetime64[ns, pytz.FixedOffset(-300)]', freq=None)
Index([2021-03-14 03:06:00-04:00, 2021-03-14 01:06:30-05:00], dtype='object')
Index([2021-03-14 03:06:00-04:00, 2021-03-14 01:06:30-05:00], dtype='object')
Index([2021-03-14 03:06:00-04:00, 2021-03-14 01:06:30-05:00], dtype='object')

Problem description

The first two lines work as expected, but the following three lines do not return a DatetimeIndex but also don't raise any exceptions in the process. This behavior is undocumented, and quite hard to debug/track down, since the failure is completely silent. It seems that the reason is the input contains times from two different time zones.

Expected Output

Either a DatetimeIndex object or an exception to be raised.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7c48ff4
python : 3.9.1.final.0
python-bits : 64
OS : Darwin
OS-release : 20.5.0
Version : Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.5
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 21.1.2
setuptools : 49.2.1
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.22.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.4.2
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions