From 9185216fcf7a1ce91942e6f339a1673513546846 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 8 Mar 2021 10:54:41 +0100 Subject: [PATCH] [ArrayManager] Fix DataFrame.from_records with emtpy size --- pandas/core/internals/construction.py | 3 +++ pandas/tests/frame/constructors/test_from_records.py | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index c314673f609f3..0b712267ccf11 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -128,6 +128,9 @@ def arrays_to_mgr( if typ == "block": return create_block_manager_from_arrays(arrays, arr_names, axes) elif typ == "array": + if len(columns) != len(arrays): + assert len(arrays) == 0 + arrays = [np.array([], dtype=object) for _ in range(len(columns))] return ArrayManager(arrays, [index, columns]) else: raise ValueError(f"'typ' needs to be one of {{'block', 'array'}}, got '{typ}'") diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index bc1007162884a..5093b88413110 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -6,7 +6,6 @@ import pytz from pandas.compat import is_platform_little_endian -import pandas.util._test_decorators as td from pandas import ( CategoricalIndex, @@ -120,7 +119,6 @@ def test_from_records_sequencelike(self): tm.assert_series_equal(result["C"], df["C"]) tm.assert_series_equal(result["E1"], df["E1"].astype("float64")) - @td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) empty from_records def test_from_records_sequencelike_empty(self): # empty case result = DataFrame.from_records([], columns=["foo", "bar", "baz"]) @@ -217,7 +215,6 @@ def __iter__(self): expected = DataFrame.from_records(tups) tm.assert_frame_equal(result, expected) - @td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) empty from_records def test_from_records_len0_with_columns(self): # GH#2633 result = DataFrame.from_records([], index="foo", columns=["foo", "bar"]) @@ -401,7 +398,6 @@ def create_dict(order_id): result = DataFrame.from_records(documents, index=["order_id", "quantity"]) assert result.index.names == ("order_id", "quantity") - @td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) empty from_records def test_from_records_misc_brokenness(self): # GH#2179 @@ -440,7 +436,6 @@ def test_from_records_misc_brokenness(self): ) tm.assert_series_equal(result, expected) - @td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) empty from_records def test_from_records_empty(self): # GH#3562 result = DataFrame.from_records([], columns=["a", "b", "c"])