BUG: to_datetime - confusion with timeawarness

This issue has been created since 2022-09-21.

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

# passes
>>> pd.to_datetime([pd.Timestamp("2022-01-01 00:00:00"), pd.Timestamp("1724-12-20 20:20:20+01:00")], utc=True)
# fails
>>> pd.to_datetime([pd.Timestamp("1724-12-20 20:20:20+01:00"), pd.Timestamp("2022-01-01 00:00:00")], utc=True)
Traceback (most recent call last):
  File "/Users/primoz/miniconda3/envs/orange3/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3398, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-69a49866a765>", line 1, in <cell line: 1>
    pd.to_datetime([pd.Timestamp("1724-12-20 20:20:20+01:00"), pd.Timestamp("2022-01-01 00:00:00")], utc=True)
  File "/Users/primoz/python-projects/pandas/pandas/core/tools/datetimes.py", line 1124, in to_datetime
    result = convert_listlike(argc, format)
  File "/Users/primoz/python-projects/pandas/pandas/core/tools/datetimes.py", line 439, in _convert_listlike_datetimes
    result, tz_parsed = objects_to_datetime64ns(
  File "/Users/primoz/python-projects/pandas/pandas/core/arrays/datetimes.py", line 2182, in objects_to_datetime64ns
    result, tz_parsed = tslib.array_to_datetime(
  File "pandas/_libs/tslib.pyx", line 428, in pandas._libs.tslib.array_to_datetime
  File "pandas/_libs/tslib.pyx", line 535, in pandas._libs.tslib.array_to_datetime
ValueError: Cannot mix tz-aware with tz-naive values

# the same examples with strings only also pass
>>> pd.to_datetime(["1724-12-20 20:20:20+01:00", "2022-01-01 00:00:00"], utc=True)

Issue Description

When calling to_datetime with pd.Timestamps in the list/series conversion fail when the datetime at position 0 is tz-aware and the other datetime is tz-naive. When datetimes are reordered the same example pass. The same example also pass when datetimes are strings.

So I am a bit confused about what is supported now. Should a case that fails work?
The error is only present in pandas==1.5.0 and in the main branch. I think it was introduced with this PR #47018.

Expected Behavior

I think to_datetime should have the same behavior in all cases.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 87cfe4e python : 3.10.4.final.0 python-bits : 64 OS : Darwin OS-release : 21.6.0 Version : Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : None LOCALE : None.UTF-8 pandas : 1.5.0 numpy : 1.22.4 pytz : 2022.1 dateutil : 2.8.2 setuptools : 61.2.0 pip : 22.1.2 Cython : None pytest : None hypothesis : None sphinx : 5.1.1 blosc : None feather : None xlsxwriter : 3.0.3 lxml.etree : 4.9.1 html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.4.0 pandas_datareader: None bs4 : 4.11.1 bottleneck : 1.3.5 brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.5.2 numba : 0.56.0 numexpr : None odfpy : None openpyxl : 3.0.10 pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : 1.9.1 snappy : None sqlalchemy : 1.4.39 tables : None tabulate : 0.8.10 xarray : None xlrd : 2.0.1 xlwt : None zstandard : None tzdata : 2022.2
MarcoGorelli wrote this answer on 2022-09-21

I think it was introduced with this PR #47018.

Can confirm (though haven't looked any more closely than that yet), thanks for the report

0277a9c49f5efb1edcb0ebdba48123e2fda2a590 is the first bad commit
commit 0277a9c49f5efb1edcb0ebdba48123e2fda2a590
Author: jbrockmendel <[email protected]>
Date:   Sat May 21 12:47:14 2022 -0700

    REF: merge datetime_to_datetime64 into array_to_datetime (#47018)

 pandas/_libs/tslib.pyx                 | 28 ++++++++++---
 pandas/_libs/tslibs/conversion.pyi     |  3 --
 pandas/_libs/tslibs/conversion.pyx     | 74 ----------------------------------
 pandas/core/arrays/datetimes.py        |  8 ----
 pandas/core/tools/datetimes.py         | 72 ++++++++++++---------------------
 pandas/tests/tools/test_to_datetime.py |  9 +++++
 6 files changed, 58 insertions(+), 136 deletions(-)
bisect run success
More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-09-29
Star Count 35374
Watcher Count 1122
Fork Count 15034
Issue Count 3579

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Suggestion: Remove text_prompts from default arguments 1 2022-07-17 2022-09-08
Erro na instalação via composer - versão 8.1 do PHP 0 2022-02-17 2022-05-21
Errors and incorrect results using variable numbers of inputs 0 2021-08-05 2022-07-20
add Readme.md to all folders 0 2021-01-29 2022-08-10
Documentation for Air purifier 1 2022-02-13 2022-08-20
Groups: groups details button is not working 0 2022-05-23 2022-05-22
Agents: remove "details" from agent 0 2022-05-26 2022-05-22
Tests: make a docker image to run behavex/behave test 0 2022-05-24 2022-08-22
Can not reconnect evicted node 1 2021-01-03 2022-08-22
ChatChannel in makeLeadingComposerView 2 2022-05-13 2022-08-22
[feat] UI for plugin 1 2022-02-13 2022-09-25
Handheld radios are broken again 5 2022-09-23 2022-09-26
Is it possible to a create a websocket server in crow that transmit continuously to a webbrowser (client) 5 2022-08-07 2022-09-16
capture for crow route lambda throws error 2 2022-08-07 2022-08-17
postMessage failed to execute 1 2021-07-12 2022-09-11
`Shift+space-swipe` cancels CAPS LOCK 1 2022-07-03 2022-09-02
Jacobian determinant images from h5 files 2 2021-04-08 2022-09-13
2.4: Rename Passage titles by clicking on them 1 2022-01-11 2022-08-30
allow the source command in dev contrib 1 2022-02-16 2022-09-18
Release test v6.46.0 2 2022-02-09 2022-09-25
These websites are no longer available :( 1 2021-05-10 2022-09-19
[Bug]: Update from 7.17.10 to 7.17.12 breaks Charka Theme in monorepo 8 2022-05-17 2022-09-26
[Bug]: Comparison with object literal fails to parse since 7.14.5 1 2022-05-14 2022-09-26
Document where on jupyter.org Jupyterlab App is listed for download 3 2021-10-03 2022-09-19
Elm Editor fails with 400 error, "request too large" 6 2021-12-17 2022-09-02
Suggestion: allow users to provide a seed in the function calls 4 2021-06-15 2022-09-28
The automated release is failing 🚨 2 2022-09-20 2022-09-18
PR to downgrade dev package is repeatedly opened and closed 3 2022-09-17 2022-09-18
Use cache to prevent cloning when onboarding repos 3 2022-09-17 2022-09-18
hostRules matchHost doesnt match git ssh urls 8 2022-09-16 2022-09-18
RPC calls not working with supabase-js and supabase-flutter. 5 2022-06-17 2022-08-24
webview on keyboard display time twice app display 2 2022-05-05 2022-09-12
Timestamp/offset ordering when consuming from multiple topics? 3 2019-11-28 2022-09-17
Add Ruby SQS example to SOS 0 2022-08-09 2022-09-19
[开源项目自荐] Hami-Vuex: 基于 Vuex,模块化设计,支持 TypeScript 的 Vue 状态管理库 0 2022-01-15 2022-07-31
mv-init backend should not log in if source has data 2 2022-08-09 2022-09-02
ProxyConfigArray in type def (types/lib/Server.d.ts) is not an Array 2 2022-01-10 2022-09-28
Custom image 1 not changing 2 2021-05-05 2022-09-21
[BUG] Where is config file? More clarity in error msg 4 2022-03-15 2022-09-18
Connection pool: How to close single connection from pool 5 2021-03-04 2022-09-17
installazione fallita su CentOS 7 1 2021-03-11 2022-09-25
[Feature Request] Search组件的自定义按钮的点击样式 7 2021-10-04 2022-08-14
[IBMPC_USB] Terminal Model M recognized as AT board 12 2021-05-31 2022-09-25
Messages not sent when cc is empty 0 2022-09-03 2022-09-27
Add tests for OVN chassis cluster member role 0 2022-03-30 2022-09-14
Error in 'addSyntheticNodes' during 'jsxClosingTag' 3 2021-08-30 2022-09-28
递归合并时触发commit-msg钩子导致verify-commit失效 0 2021-10-12 2022-09-06
Error updating notebooks 0 2022-07-10 2022-08-02
Modify extra variables with CSS Variables (Dynamic Theme) 1 2021-12-06 2022-08-05
Bug: `eslint-plugin-react-hooks` cannot detect `useRef` dependencies. 1 2022-03-01 2022-05-28