Pandas version checks

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

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

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

Reproducible Example

import pandas as pd
from sqlalchemy import (
    create_engine,
    Table,
    Column,
)
from sqlalchemy.types import (
    BigInteger,
)
from sqlalchemy.sql import (
    select,
)
from sqlalchemy.orm import (
    declarative_base,
)

table_name = 'test'

Base = declarative_base()

class Jobs(Base):
    __table__ = Table(
        table_name,
        Base.metadata,
        Column('id', BigInteger, primary_key=True),
        Column('a', BigInteger),
        Column('b', BigInteger),
    )

df = pd.DataFrame(
    {
        'id': [1],
        'a': [2],
        'b': [3],
    },
    dtype='int64',
)

engine = create_engine('sqlite:///:memory:')

df.to_sql(
    table_name,
    engine,
    index=False,
    if_exists='replace',
)


# This results in the pickled dataframe dependent on sqlalchemy
df1 = pd.read_sql_query(
    select(Jobs),
    engine,
)

# This dataframe is not dependent on sqlalchemy
df2 = pd.read_sql_query(
    f'SELECT * FROM {table_name}',
    engine,
)

assert df1.equals(df2)

df1.to_pickle('test1.pkl')
df2.to_pickle('test2.pkl')

Issue Description

read_sql_query() returns subtly different dataframes depending on the type of the sql parameter used.

Although the two dataframes are equal to each other, their difference manifests when they get pickled.

If the sql parameter is an SQLAlchemy Selectable, the resulting dataframe contains a dependency on the sqlalchemy module.

Unless sqlalchemy is installed in the Python virtual environment, such a pickled dataframe cannot be read. The error message states: ModuleNotFoundError: No module named 'sqlalchemy'

Expected Behavior

I expect that both test1.pkl and test2.pkl should be possible to read in the virtual environment where only Pandas is installed.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 478d340667831908b5b4bf09a2787a11a14560c9 python : 3.9.16.final.0 python-bits : 64 OS : Linux OS-release : 5.10.102.1-microsoft-standard-WSL2 Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : en_US.UTF-8 LANG : en_US.UTF-8 LOCALE : en_US.UTF-8 pandas : 2.0.0 numpy : 1.24.2 pytz : 2023.3 dateutil : 2.8.2 setuptools : 58.1.0 pip : 23.0.1 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : 2.9.6 jinja2 : None IPython : 8.12.0 pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.7.1 numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : 1.4.44 tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.3 qtpy : None pyqt5 : None

Comment From: NumanIjaz

take