Skip to content

BUG: to_sql raises when arrow dtype has missing values #52180

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 2 commits into from
Mar 24, 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
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2093,7 +2093,7 @@ def _dt_round(
def _dt_to_pydatetime(self):
data = self._pa_array.to_pylist()
if self._dtype.pyarrow_dtype.unit == "ns":
data = [ts.to_pydatetime(warn=False) for ts in data]
data = [None if ts is None else ts.to_pydatetime(warn=False) for ts in data]
return np.array(data, dtype=object)

def _dt_tz_localize(
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,22 @@ def test_dataframe_to_sql_arrow_dtypes(conn, request):
df.to_sql("test_arrow", conn, if_exists="replace", index=False)


@pytest.mark.db
@pytest.mark.parametrize("conn", all_connectable)
def test_dataframe_to_sql_arrow_dtypes_missing(conn, request, nulls_fixture):
# GH 52046
pytest.importorskip("pyarrow")
df = DataFrame(
{
"datetime": pd.array(
[datetime(2023, 1, 1), nulls_fixture], dtype="timestamp[ns][pyarrow]"
),
}
)
conn = request.getfixturevalue(conn)
df.to_sql("test_arrow", conn, if_exists="replace", index=False)


@pytest.mark.db
@pytest.mark.parametrize("conn", all_connectable)
@pytest.mark.parametrize("method", [None, "multi"])
Expand Down