Skip to content

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 28 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4d44e84
Start sql implementation
phofl Dec 3, 2022
aff00f1
BUG: Fix bug in maybe_convert_objects with None and nullable
phofl Dec 3, 2022
9070acd
Add gh ref
phofl Dec 3, 2022
a66e8bd
Merge branch 'maybe_convert_objects' into sql
phofl Dec 3, 2022
f69d6d8
Continue sql implementation
phofl Dec 3, 2022
3d6958d
ENH: maybe_convert_objects add boolean support with NA
phofl Dec 3, 2022
48f41a2
Merge remote-tracking branch 'upstream/main' into maybe_convert_objec…
phofl Dec 3, 2022
43545c5
Fix merge error
phofl Dec 3, 2022
1ed72bf
Add gh ref
phofl Dec 3, 2022
9675341
Merge remote-tracking branch 'upstream/main' into sql
phofl Dec 3, 2022
e5bec77
Merge branch 'maybe_convert_objects_nullable_boolean' into sql
phofl Dec 3, 2022
4e93577
Add to api
phofl Dec 3, 2022
d5d0047
Add tests
phofl Dec 3, 2022
62c798f
Fix test
phofl Dec 3, 2022
a8e7fcd
Merge branch 'maybe_convert_objects_nullable_boolean' into sql
phofl Dec 3, 2022
da4f4ae
Fix test
phofl Dec 4, 2022
85c995a
Simplify
phofl Dec 4, 2022
8f6b859
Merge branch 'maybe_convert_objects_nullable_boolean' into sql
phofl Dec 4, 2022
87743f3
Implement string support
phofl Dec 4, 2022
3363466
Add support for table
phofl Dec 4, 2022
a0233cd
Add docstring
phofl Dec 4, 2022
58561d7
Add whatsnew
phofl Dec 4, 2022
6540859
Fix tests
phofl Dec 4, 2022
0b882ae
Merge remote-tracking branch 'upstream/main' into sql
phofl Dec 4, 2022
80b61e3
Fix pylint
phofl Dec 5, 2022
940bca7
Fix docstring
phofl Dec 5, 2022
839ef57
Merge remote-tracking branch 'upstream/main' into sql
phofl Dec 9, 2022
e67a0c8
Merge remote-tracking branch 'upstream/main' into sql
phofl Dec 10, 2022
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 doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The ``use_nullable_dtypes`` keyword argument has been expanded to the following

* :func:`read_csv`
* :func:`read_excel`
* :func:`read_sql`

Additionally a new global configuration, ``io.nullable_backend`` can now be used in conjunction with the parameter ``use_nullable_dtypes=True`` in the following functions
to select the nullable dtypes implementation.
Expand Down
38 changes: 33 additions & 5 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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)
Copy link
Member

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?

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):
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
Loading