Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this issue exists on the latest version of pandas.
-
I have confirmed this issue exists on the main branch of pandas.
Reproducible Example
I have been seeing slow performance when using the pandas read_sql_query function when the returned data includes datetimes with a non UTC timezone offset.
I have located the issue to the objects_to_datetime64ns function called when constructing the dataframe.
In pandas 1.4.* the data function would try to call tslib.array_to_datetime and fail, then having a failover to conversion.datetime_to_datetime64, but for pandas 1.5.* the tslib.array_to_datetime is executed which takes a significant amount of time compared to the 1.4.* execution time.
Installed Versions
INSTALLED VERSIONS
commit : 8dab54d
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22000
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Danish_Denmark.1252
pandas : 1.5.2
numpy : 1.24.1
pytz : 2022.7
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
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 : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
Prior Performance
On my computer, the difference between the two version is
Pandas 1.4.4: Execution time: 0:00:00.013004
Pandas 1.5.2: Execution time: 0:00:21.586688
import pandas as pd
import datetime as dt
import pytz
import logging
from pandas._libs.tslibs import (
conversion,
)
from pandas._libs import (
tslib,
)
date_range = pd.date_range(dt.datetime(2021, 1, 1, tzinfo=pytz.timezone('CET')), dt.datetime(2022, 1, 1, tzinfo=pytz.timezone('CET')), freq='1H')
df = pd.DataFrame({'dtColumn': date_range})
df['dtColumn'] = pd.to_datetime(df['dtColumn'])
data = df.iloc[:, 0:].values
print('Starting conversion')
if pd.__version__ == '1.5.2':
start = dt.datetime.now()
result, tz_parsed = tslib.array_to_datetime(data.ravel("K"),
errors='raise',
utc=False,
dayfirst=False,
yearfirst=False,
require_iso8601=True,
allow_mixed=True)
print(f'Execution time: {dt.datetime.now() - start}')
if pd.__version__ == '1.4.4':
start = dt.datetime.now()
try:
result, tz_parsed = tslib.array_to_datetime(data.ravel("K"),
errors='raise',
utc=False,
dayfirst=False,
yearfirst=False,
require_iso8601=True,
allow_mixed=True)
except ValueError:
values, tz_parsed = conversion.datetime_to_datetime64(data.ravel("K"))
print(f'Execution time: {dt.datetime.now() - start}')