-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add nullable keyword to read_sql #50048
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
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4d44e84
Start sql implementation
phofl aff00f1
BUG: Fix bug in maybe_convert_objects with None and nullable
phofl 9070acd
Add gh ref
phofl a66e8bd
Merge branch 'maybe_convert_objects' into sql
phofl f69d6d8
Continue sql implementation
phofl 3d6958d
ENH: maybe_convert_objects add boolean support with NA
phofl 48f41a2
Merge remote-tracking branch 'upstream/main' into maybe_convert_objec…
phofl 43545c5
Fix merge error
phofl 1ed72bf
Add gh ref
phofl 9675341
Merge remote-tracking branch 'upstream/main' into sql
phofl e5bec77
Merge branch 'maybe_convert_objects_nullable_boolean' into sql
phofl 4e93577
Add to api
phofl d5d0047
Add tests
phofl 62c798f
Fix test
phofl a8e7fcd
Merge branch 'maybe_convert_objects_nullable_boolean' into sql
phofl da4f4ae
Fix test
phofl 85c995a
Simplify
phofl 8f6b859
Merge branch 'maybe_convert_objects_nullable_boolean' into sql
phofl 87743f3
Implement string support
phofl 3363466
Add support for table
phofl a0233cd
Add docstring
phofl 58561d7
Add whatsnew
phofl 6540859
Fix tests
phofl 0b882ae
Merge remote-tracking branch 'upstream/main' into sql
phofl 80b61e3
Fix pylint
phofl 940bca7
Fix docstring
phofl 839ef57
Merge remote-tracking branch 'upstream/main' into sql
phofl e67a0c8
Merge remote-tracking branch 'upstream/main' into sql
phofl 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 hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -31,9 +31,11 @@ | |
) | ||
from pandas.core.dtypes.common import ( | ||
is_1d_only_ea_dtype, | ||
is_bool_dtype, | ||
is_datetime_or_timedelta_dtype, | ||
is_dtype_equal, | ||
is_extension_array_dtype, | ||
is_float_dtype, | ||
is_integer_dtype, | ||
is_list_like, | ||
is_named_tuple, | ||
|
@@ -49,7 +51,13 @@ | |
algorithms, | ||
common as com, | ||
) | ||
from pandas.core.arrays import ExtensionArray | ||
from pandas.core.arrays import ( | ||
BooleanArray, | ||
ExtensionArray, | ||
FloatingArray, | ||
IntegerArray, | ||
) | ||
from pandas.core.arrays.string_ import StringDtype | ||
from pandas.core.construction import ( | ||
ensure_wrapped_if_datetimelike, | ||
extract_array, | ||
|
@@ -900,7 +908,7 @@ def _finalize_columns_and_data( | |
raise ValueError(err) from err | ||
|
||
if len(contents) and contents[0].dtype == np.object_: | ||
contents = _convert_object_array(contents, dtype=dtype) | ||
contents = convert_object_array(contents, dtype=dtype) | ||
|
||
return contents, columns | ||
|
||
|
@@ -963,8 +971,11 @@ def _validate_or_indexify_columns( | |
return columns | ||
|
||
|
||
def _convert_object_array( | ||
content: list[npt.NDArray[np.object_]], dtype: DtypeObj | None | ||
def convert_object_array( | ||
content: list[npt.NDArray[np.object_]], | ||
dtype: DtypeObj | None, | ||
use_nullable_dtypes: bool = False, | ||
coerce_float: bool = False, | ||
) -> list[ArrayLike]: | ||
""" | ||
Internal function to convert object array. | ||
|
@@ -973,20 +984,37 @@ def _convert_object_array( | |
---------- | ||
content: List[np.ndarray] | ||
dtype: np.dtype or ExtensionDtype | ||
use_nullable_dtypes: Controls if nullable dtypes are returned. | ||
coerce_float: Cast floats that are integers to int. | ||
|
||
Returns | ||
------- | ||
List[ArrayLike] | ||
""" | ||
# provide soft conversion of object dtypes | ||
|
||
def convert(arr): | ||
if dtype != np.dtype("O"): | ||
arr = lib.maybe_convert_objects(arr) | ||
arr = lib.maybe_convert_objects( | ||
arr, | ||
try_float=coerce_float, | ||
convert_to_nullable_dtype=use_nullable_dtypes, | ||
) | ||
|
||
if dtype is None: | ||
if arr.dtype == np.dtype("O"): | ||
# i.e. maybe_convert_objects didn't convert | ||
arr = maybe_infer_to_datetimelike(arr) | ||
if use_nullable_dtypes and arr.dtype == np.dtype("O"): | ||
arr = StringDtype().construct_array_type()._from_sequence(arr) | ||
elif use_nullable_dtypes and isinstance(arr, np.ndarray): | ||
if is_integer_dtype(arr.dtype): | ||
arr = IntegerArray(arr, np.zeros(arr.shape, dtype=np.bool_)) | ||
elif is_bool_dtype(arr.dtype): | ||
arr = BooleanArray(arr, np.zeros(arr.shape, dtype=np.bool_)) | ||
elif is_float_dtype(arr.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. could re-use pd.array for L1011-L1016? |
||
arr = FloatingArray(arr, np.isnan(arr)) | ||
|
||
elif isinstance(dtype, ExtensionDtype): | ||
# TODO: test(s) that get here | ||
# TODO: try to de-duplicate this convert function with | ||
|
Oops, something went wrong.
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.
this seems weird to me. why are we casting potentially non-strings to strings?