Skip to content

GH - 624: Added dtype arg to read_sql #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class FulldatetimeDict(YearMonthDayDict, total=False):
NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object]
Dtype: TypeAlias = ExtensionDtype | NpDtype
DtypeArg: TypeAlias = Dtype | dict[Any, Dtype]
DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"]
BooleanDtypeArg: TypeAlias = (
# Builtin bool type and its string alias
type[bool] # noqa: Y030
Expand Down
6 changes: 6 additions & 0 deletions pandas-stubs/io/sql.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import sqlalchemy.engine
import sqlalchemy.sql.expression
from typing_extensions import TypeAlias

from pandas._libs.lib import NoDefault
from pandas._typing import (
DtypeArg,
DtypeBackend,
npt,
)

Expand Down Expand Up @@ -84,6 +86,8 @@ def read_sql(
columns: list[str] = ...,
*,
chunksize: int,
dtype: DtypeArg | None = ...,
dtype_backend: DtypeBackend | NoDefault = ...,
) -> Generator[DataFrame, None, None]: ...
@overload
def read_sql(
Expand All @@ -95,6 +99,8 @@ def read_sql(
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
columns: list[str] = ...,
chunksize: None = ...,
dtype: DtypeArg | None = ...,
dtype_backend: DtypeBackend | NoDefault = ...,
) -> DataFrame: ...

class PandasSQL(PandasObject):
Expand Down
65 changes: 65 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,3 +1204,68 @@ def test_sqlalchemy_text() -> None:
assert_type(read_sql(sql_select, con=conn), DataFrame),
DataFrame,
)


def test_read_sql_dtype() -> None:
with ensure_clean() as path:
conn = sqlite3.connect(path)
df = pd.DataFrame(
data=[[0, "10/11/12"], [1, "12/11/10"]],
columns=["int_column", "date_column"],
)
check(assert_type(df.to_sql("test_data", con=conn), Union[int, None]), int)
check(
assert_type(
pd.read_sql(
"SELECT int_column, date_column FROM test_data",
con=conn,
dtype=None,
),
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(
pd.read_sql(
"SELECT int_column, date_column FROM test_data",
con=conn,
dtype={"int_column": int},
),
pd.DataFrame,
),
pd.DataFrame,
)
check(assert_type(DF.to_sql("test", con=conn), Union[int, None]), int)

check(
assert_type(
read_sql("select * from test", con=conn, dtype=int),
pd.DataFrame,
),
pd.DataFrame,
)
conn.close()


def test_read_sql_dtype_backend() -> None:
with ensure_clean() as path:
conn2 = sqlite3.connect(path)
check(assert_type(DF.to_sql("test", con=conn2), Union[int, None]), int)
check(
assert_type(
read_sql("select * from test", con=conn2, dtype_backend="pyarrow"),
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(
read_sql(
"select * from test", con=conn2, dtype_backend="numpy_nullable"
),
pd.DataFrame,
),
pd.DataFrame,
)
conn2.close()