(
values, index, columns, dtype: DtypeObj | None, copy: bool
)
| 191 | |
| 192 | |
| 193 | def ndarray_to_mgr( |
| 194 | values, index, columns, dtype: DtypeObj | None, copy: bool |
| 195 | ) -> Manager: |
| 196 | # used in DataFrame.__init__ |
| 197 | # input must be an ndarray, list, Series, Index, ExtensionArray |
| 198 | infer_object = not isinstance(values, (ABCSeries, Index, ExtensionArray)) |
| 199 | |
| 200 | if isinstance(values, ABCSeries): |
| 201 | if columns is None: |
| 202 | if values.name is not None: |
| 203 | columns = Index([values.name]) |
| 204 | if index is None: |
| 205 | index = values.index |
| 206 | else: |
| 207 | values = values.reindex(index) |
| 208 | |
| 209 | # zero len case (GH #2234) |
| 210 | if not len(values) and columns is not None and len(columns): |
| 211 | values = np.empty((0, 1), dtype=object) |
| 212 | |
| 213 | vdtype = getattr(values, "dtype", None) |
| 214 | refs = None |
| 215 | if is_1d_only_ea_dtype(vdtype) or is_1d_only_ea_dtype(dtype): |
| 216 | # GH#19157 |
| 217 | |
| 218 | if isinstance(values, (np.ndarray, ExtensionArray)) and values.ndim > 1: |
| 219 | # GH#12513 an EA dtype passed with a 2D array, split into |
| 220 | # multiple EAs that view the values |
| 221 | # error: No overload variant of "__getitem__" of "ExtensionArray" |
| 222 | # matches argument type "Tuple[slice, int]" |
| 223 | values = [ |
| 224 | values[:, n] # type: ignore[call-overload] |
| 225 | for n in range(values.shape[1]) |
| 226 | ] |
| 227 | else: |
| 228 | values = [values] |
| 229 | |
| 230 | # Handle copy semantics: already copy 1d-only EA. Other arrays will |
| 231 | # be copied when consolidating the blocks |
| 232 | if copy: |
| 233 | values = [ |
| 234 | (x.copy(deep=True) if isinstance(x, Index) else x.copy()) |
| 235 | if isinstance(x, (ExtensionArray, Index, ABCSeries)) |
| 236 | and is_1d_only_ea_dtype(x.dtype) |
| 237 | else x |
| 238 | for x in values |
| 239 | ] |
| 240 | |
| 241 | if columns is None: |
| 242 | columns = Index(range(len(values))) |
| 243 | else: |
| 244 | columns = ensure_index(columns) |
| 245 | |
| 246 | return arrays_to_mgr(values, columns, index, dtype=dtype, consolidate=copy) |
| 247 | |
| 248 | if isinstance(values, (ABCSeries, Index)): |
| 249 | if not copy and (dtype is None or astype_is_view(values.dtype, dtype)): |
| 250 | refs = values._references |
no test coverage detected