Skip to content

Commit

Permalink
Backport PR #52180 on branch 2.0.x (BUG: to_sql raises when arrow dty…
Browse files Browse the repository at this point in the history
…pe has missing values) (#52185)

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

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Mar 24, 2023
1 parent 19a5f38 commit 27a8380
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ def _dt_round(
def _dt_to_pydatetime(self):
data = self._data.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

0 comments on commit 27a8380

Please sign in to comment.