-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
CLN: Add typing for dtype argument in io/sql.py #38680
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e1bc42a
Add typing for io/sql.py
avinashpancham 1aaa6a2
Merge remote-tracking branch 'upstream/master' into typing_dtype
avinashpancham 2c70647
Cast dtype to dict
avinashpancham a82a6c1
Replace isinstance checks with is_dict_like
avinashpancham 41138e3
Merge remote-tracking branch 'upstream/master' into typing_dtype
avinashpancham 8dc13bd
Revert check to original form
avinashpancham ae41d91
Remove superfluous if statement
avinashpancham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
from datetime import date, datetime, time | ||
from functools import partial | ||
import re | ||
from typing import Any, Dict, Iterator, List, Optional, Sequence, Union, overload | ||
from typing import Any, Dict, Iterator, List, Optional, Sequence, Union, cast, overload | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -383,6 +383,8 @@ def read_sql_query( | |
Data type for data or columns. E.g. np.float64 or | ||
{‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’} | ||
|
||
.. versionadded:: 1.3.0 | ||
|
||
Returns | ||
------- | ||
DataFrame or Iterator[DataFrame] | ||
|
@@ -609,7 +611,7 @@ def to_sql( | |
index: bool = True, | ||
index_label=None, | ||
chunksize: Optional[int] = None, | ||
dtype=None, | ||
dtype: Optional[DtypeArg] = None, | ||
method: Optional[str] = None, | ||
) -> None: | ||
""" | ||
|
@@ -768,7 +770,7 @@ def __init__( | |
index_label=None, | ||
schema=None, | ||
keys=None, | ||
dtype=None, | ||
dtype: Optional[DtypeArg] = None, | ||
): | ||
self.name = name | ||
self.pd_sql = pandas_sql_engine | ||
|
@@ -1108,9 +1110,9 @@ def _harmonize_columns(self, parse_dates=None): | |
|
||
def _sqlalchemy_type(self, col): | ||
|
||
dtype = self.dtype or {} | ||
if col.name in dtype: | ||
return self.dtype[col.name] | ||
dtype: DtypeArg = self.dtype or {} | ||
if isinstance(dtype, dict) and col.name in dtype: | ||
return dtype[col.name] | ||
|
||
# Infer type of column, while ignoring missing values. | ||
# Needed for inserting typed data containing NULLs, GH 8778. | ||
|
@@ -1209,7 +1211,18 @@ def read_sql(self, *args, **kwargs): | |
"connectable or sqlite connection" | ||
) | ||
|
||
def to_sql(self, *args, **kwargs): | ||
def to_sql( | ||
self, | ||
frame, | ||
name, | ||
if_exists="fail", | ||
index=True, | ||
index_label=None, | ||
schema=None, | ||
chunksize=None, | ||
dtype: Optional[DtypeArg] = None, | ||
method=None, | ||
): | ||
raise ValueError( | ||
"PandasSQL must be created with an SQLAlchemy " | ||
"connectable or sqlite connection" | ||
|
@@ -1436,7 +1449,7 @@ def to_sql( | |
index_label=None, | ||
schema=None, | ||
chunksize=None, | ||
dtype=None, | ||
dtype: Optional[DtypeArg] = None, | ||
method=None, | ||
): | ||
""" | ||
|
@@ -1480,10 +1493,13 @@ def to_sql( | |
|
||
.. versionadded:: 0.24.0 | ||
""" | ||
if dtype and not is_dict_like(dtype): | ||
dtype = {col_name: dtype for col_name in frame} | ||
|
||
if dtype is not None: | ||
if not is_dict_like(dtype): | ||
dtype = {col_name: dtype for col_name in frame} | ||
else: | ||
dtype = cast(dict, dtype) | ||
|
||
if dtype: | ||
from sqlalchemy.types import TypeEngine, to_instance | ||
|
||
for col, my_type in dtype.items(): | ||
|
@@ -1569,7 +1585,7 @@ def _create_sql_schema( | |
frame: DataFrame, | ||
table_name: str, | ||
keys: Optional[List[str]] = None, | ||
dtype: Optional[dict] = None, | ||
dtype: Optional[DtypeArg] = None, | ||
schema: Optional[str] = None, | ||
): | ||
table = SQLTable( | ||
|
@@ -1740,8 +1756,8 @@ def _create_table_setup(self): | |
return create_stmts | ||
|
||
def _sql_type_name(self, col): | ||
dtype = self.dtype or {} | ||
if col.name in dtype: | ||
dtype: DtypeArg = self.dtype or {} | ||
if isinstance(dtype, dict) and col.name in dtype: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use is_dict_like |
||
return dtype[col.name] | ||
|
||
# Infer type of column, while ignoring missing values. | ||
|
@@ -1901,7 +1917,7 @@ def to_sql( | |
index_label=None, | ||
schema=None, | ||
chunksize=None, | ||
dtype=None, | ||
dtype: Optional[DtypeArg] = None, | ||
method=None, | ||
): | ||
""" | ||
|
@@ -1947,7 +1963,7 @@ def to_sql( | |
if dtype and not is_dict_like(dtype): | ||
dtype = {col_name: dtype for col_name in frame} | ||
|
||
if dtype is not None: | ||
if dtype is not None and isinstance(dtype, dict): | ||
for col, my_type in dtype.items(): | ||
if not isinstance(my_type, str): | ||
raise ValueError(f"{col} ({my_type}) not a string") | ||
|
@@ -1986,7 +2002,7 @@ def _create_sql_schema( | |
frame, | ||
table_name: str, | ||
keys=None, | ||
dtype=None, | ||
dtype: Optional[DtypeArg] = None, | ||
schema: Optional[str] = None, | ||
): | ||
table = SQLiteTable( | ||
|
@@ -2002,7 +2018,12 @@ def _create_sql_schema( | |
|
||
|
||
def get_schema( | ||
frame, name: str, keys=None, con=None, dtype=None, schema: Optional[str] = None | ||
frame, | ||
name: str, | ||
keys=None, | ||
con=None, | ||
dtype: Optional[DtypeArg] = None, | ||
schema: Optional[str] = None, | ||
): | ||
""" | ||
Get the SQL db table schema for the given frame. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shoudl is is_dict_like