@@ -297,6 +297,8 @@ def ndarray_to_mgr(
297
297
)
298
298
values = values .T
299
299
300
+ _check_values_indices_shape_match (values , index , columns )
301
+
300
302
# if we don't have a dtype specified, then try to convert objects
301
303
# on the entire block; this is to convert if we have datetimelike's
302
304
# embedded in an object type
@@ -318,15 +320,37 @@ def ndarray_to_mgr(
318
320
else :
319
321
datelike_vals = maybe_infer_to_datetimelike (values )
320
322
datelike_vals = maybe_squeeze_dt64tz (datelike_vals )
321
- block_values = [datelike_vals ]
323
+ nb = new_block (datelike_vals , placement = slice (len (columns )), ndim = 2 )
324
+ block_values = [nb ]
322
325
else :
323
- # error: List item 0 has incompatible type "Union[ExtensionArray, ndarray]";
324
- # expected "Block"
325
- block_values = [maybe_squeeze_dt64tz (values )] # type: ignore[list-item]
326
+ new_values = maybe_squeeze_dt64tz (values )
327
+ nb = new_block (new_values , placement = slice (len (columns )), ndim = 2 )
328
+ block_values = [nb ]
329
+
330
+ if len (columns ) == 0 :
331
+ block_values = []
326
332
327
333
return create_block_manager_from_blocks (block_values , [columns , index ])
328
334
329
335
336
+ def _check_values_indices_shape_match (
337
+ values : np .ndarray , index : Index , columns : Index
338
+ ) -> None :
339
+ """
340
+ Check that the shape implied by our axes matches the actual shape of the
341
+ data.
342
+ """
343
+ if values .shape [0 ] != len (columns ):
344
+ # Could let this raise in Block constructor, but we get a more
345
+ # helpful exception message this way.
346
+ if values .shape [1 ] == 0 :
347
+ raise ValueError ("Empty data passed with indices specified." )
348
+
349
+ passed = values .T .shape
350
+ implied = (len (index ), len (columns ))
351
+ raise ValueError (f"Shape of passed values is { passed } , indices imply { implied } " )
352
+
353
+
330
354
def maybe_squeeze_dt64tz (dta : ArrayLike ) -> ArrayLike :
331
355
"""
332
356
If we have a tzaware DatetimeArray with shape (1, N), squeeze to (N,)
0 commit comments