Skip to content

Commit b195a67

Browse files
authored
REF: sql insert_data operate column-wise to avoid internals (#33229)
1 parent 77b03bd commit b195a67

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

pandas/io/sql.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -692,37 +692,25 @@ def insert_data(self):
692692
column_names = list(map(str, temp.columns))
693693
ncols = len(column_names)
694694
data_list = [None] * ncols
695-
blocks = temp._mgr.blocks
696-
697-
for b in blocks:
698-
if b.is_datetime:
699-
# return datetime.datetime objects
700-
if b.is_datetimetz:
701-
# GH 9086: Ensure we return datetimes with timezone info
702-
# Need to return 2-D data; DatetimeIndex is 1D
703-
d = b.values.to_pydatetime()
704-
d = np.atleast_2d(d)
705-
else:
706-
# convert to microsecond resolution for datetime.datetime
707-
d = b.values.astype("M8[us]").astype(object)
708-
elif b.is_timedelta:
709-
# numpy converts this to an object array of integers,
710-
# whereas b.astype(object).values would convert to
711-
# object array of Timedeltas
712-
d = b.values.astype(object)
695+
696+
for i, (_, ser) in enumerate(temp.items()):
697+
vals = ser._values
698+
if vals.dtype.kind == "M":
699+
d = vals.to_pydatetime()
700+
elif vals.dtype.kind == "m":
701+
# store as integers, see GH#6921, GH#7076
702+
d = vals.view("i8").astype(object)
713703
else:
714-
# TODO(2DEA): astype-first can be avoided with 2D EAs
715-
# astype on the block instead of values to ensure we
716-
# get the right shape
717-
d = b.astype(object).values
704+
d = vals.astype(object)
705+
706+
assert isinstance(d, np.ndarray), type(d)
718707

719-
# replace NaN with None
720-
if b._can_hold_na:
708+
if ser._can_hold_na:
709+
# Note: this will miss timedeltas since they are converted to int
721710
mask = isna(d)
722711
d[mask] = None
723712

724-
for col_loc, col in zip(b.mgr_locs, d):
725-
data_list[col_loc] = col
713+
data_list[i] = d
726714

727715
return column_names, data_list
728716

0 commit comments

Comments
 (0)