8000 ENH: Add dtype argument to read_sql_query (GH10285) by avinashpancham · Pull Request #37546 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add dtype argument to read_sql_query (GH10285) #37546

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 12 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Address comments
  • Loading branch information
avinashpancham committed Dec 23, 2020
commit a4e7cdf4e90255ee3d20611f19e045db8f84ea2a
4 changes: 2 additions & 2 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@
Axes = Collection

# dtypes

Dtype = Union[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment on what DtypeArg is / supposed to be used

"ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool, object]]
]
DtypeArg = Optional[Union[Dtype, Dict[Label, Dtype]]]
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
DtypeArg = Union[Dtype, Dict[Label, Dtype]]
DtypeObj = Union[np.dtype, "ExtensionDtype"]

# For functions like rename that convert one label to another
Copy link
Contributor Author
@avinashpancham avinashpancham Dec 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the dtype block since Label was defined later in the file

Expand Down
18 changes: 10 additions & 8 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _wrap_result(
index_col=None,
coerce_float: bool = True,
parse_dates=None,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
):
"""Wrap result set of query in a DataFrame."""
frame = DataFrame.from_records(data, columns=columns, coerce_float=coerce_float)
Expand Down Expand Up @@ -313,7 +313,7 @@ def read_sql_query(
params=None,
parse_dates=None,
chunksize: None = None,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
) -> DataFrame:
...

Expand All @@ -327,7 +327,7 @@ def read_sql_query(
params=None,
parse_dates=None,
chunksize: int = 1,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
) -> Iterator[DataFrame]:
...

Expand All @@ -340,7 +340,7 @@ def read_sql_query(
params=None,
parse_dates=None,
chunksize: Optional[int] = None,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
) -> Union[DataFrame, Iterator[DataFrame]]:
"""
Read SQL query into a DataFrame.
Expand Down Expand Up @@ -1319,7 +1319,7 @@ def _query_iterator(
index_col=None,
coerce_float=True,
parse_dates=None,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
):
"""Return generator through chunked result set"""
while True:
Expand All @@ -1344,7 +1344,7 @@ def read_query(
parse_dates=None,
params=None,
chunksize: Optional[int] = None,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
):
"""
Read SQL query into a DataFrame.
Expand Down Expand Up @@ -1380,6 +1380,8 @@ def read_query(
Data type for data or columns. E.g. np.float64 or
{‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

versionadded 1.3


.. versionadded:: 1.3.0

Returns
-------
DataFrame
Expand Down Expand Up @@ -1819,7 +1821,7 @@ def _query_iterator(
index_col=None,
coerce_float: bool = True,
parse_dates=None,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
):
"""Return generator through chunked result set"""
while True:
Expand Down Expand Up @@ -1847,7 +1849,7 @@ def read_query(
params=None,
parse_dates=None,
chunksize: Optional[int] = None,
dtype: DtypeArg = None,
dtype: Optional[DtypeArg] = None,
):

args = _convert_params(sql, params)
Expand Down
24 changes: 13 additions & 11 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,23 +938,25 @@ def test_multiindex_roundtrip(self):
tm.assert_frame_equal(df, result, check_index_type=True)

@pytest.mark.parametrize(
"dtype, expected",
"dtype",
[
(None, [float, float]),
(int, [int, int]),
(float, [float, float]),
({"SepalLength": int, "SepalWidth": float}, [int, float]),
None,
int,
float,
{"A": int, "B": float},
],
)
def test_dtype_argument(self, dtype, expected):
def test_dtype_argument(self, dtype):
# GH10285 Add dtype argument to read_sql_query
df = DataFrame([[1.2, 3.4], [5.6, 7.8]], columns=["A", "B"])
df.to_sql("test_dtype_argument", self.conn)

expected = df.astype(dtype)
result = sql.read_sql_query(
"SELECT SepalLength, SepalWidth FROM iris", self.conn, dtype=dtype
"SELECT A, B FROM test_dtype_argument", con=self.conn, dtype=dtype
)
assert result.dtypes.to_dict() == {
"SepalLength": expected[0],
"SepalWidth": expected[1],
}

tm.assert_frame_equal(result, expected)

def test_integer_col_names(self):
df = DataFrame([[1, 2], [3, 4]], columns=[0, 1])
Expand Down
0