@@ -297,6 +297,8 @@ def ndarray_to_mgr(
297297 )
298298 values = values .T
299299
300+ _check_values_indices_shape_match (values , index , columns )
301+
300302 # if we don't have a dtype specified, then try to convert objects
301303 # on the entire block; this is to convert if we have datetimelike's
302304 # embedded in an object type
@@ -318,15 +320,37 @@ def ndarray_to_mgr(
318320 else :
319321 datelike_vals = maybe_infer_to_datetimelike (values )
320322 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 ]
322325 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 = []
326332
327333 return create_block_manager_from_blocks (block_values , [columns , index ])
328334
329335
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+
330354def maybe_squeeze_dt64tz (dta : ArrayLike ) -> ArrayLike :
331355 """
332356 If we have a tzaware DatetimeArray with shape (1, N), squeeze to (N,)
0 commit comments