MCPcopy
hub / github.com/pandas-dev/pandas / insert_data

Method insert_data

pandas/io/sql.py:1034–1081  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1032 return result.rowcount
1033
1034 def insert_data(self) -> tuple[list[str], list[np.ndarray]]:
1035 if self.index is not None:
1036 temp = self.frame.copy(deep=False)
1037 temp.index.names = self.index
1038 try:
1039 temp.reset_index(inplace=True)
1040 except ValueError as err:
1041 raise ValueError(f"duplicate name in index/columns: {err}") from err
1042 else:
1043 temp = self.frame
1044
1045 column_names = list(map(str, temp.columns))
1046 ncols = len(column_names)
1047 # this just pre-allocates the list: None's will be replaced with ndarrays
1048 # error: List item 0 has incompatible type "None"; expected "ndarray"
1049 data_list: list[np.ndarray] = [None] * ncols # type: ignore[list-item]
1050
1051 for i, (_, ser) in enumerate(temp.items()):
1052 if ser.dtype.kind == "M":
1053 if isinstance(ser._values, ArrowExtensionArray):
1054 import pyarrow as pa
1055
1056 if pa.types.is_date(ser.dtype.pyarrow_dtype):
1057 # GH#53854 to_pydatetime not supported for pyarrow date dtypes
1058 d = ser._values.to_numpy(dtype=object)
1059 else:
1060 d = ser.dt.to_pydatetime()._values
1061 else:
1062 d = ser._values.to_pydatetime()
1063 elif ser.dtype.kind == "m":
1064 vals = ser._values
1065 if isinstance(vals, ArrowExtensionArray):
1066 vals = vals.to_numpy(dtype=np.dtype("m8[ns]"))
1067 # store as integers, see GH#6921, GH#7076
1068 d = vals.view("i8").astype(object)
1069 else:
1070 d = ser._values.astype(object)
1071
1072 assert isinstance(d, np.ndarray), type(d)
1073
1074 if ser._can_hold_na:
1075 # Note: this will miss timedeltas since they are converted to int
1076 mask = isna(d)
1077 d[mask] = None
1078
1079 data_list[i] = d
1080
1081 return column_names, data_list
1082
1083 def insert(
1084 self,

Callers 1

insertMethod · 0.95

Calls 9

isnaFunction · 0.90
copyMethod · 0.45
reset_indexMethod · 0.45
itemsMethod · 0.45
to_numpyMethod · 0.45
to_pydatetimeMethod · 0.45
dtypeMethod · 0.45
astypeMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected