Skip to content

Unit Test: Construction of DataFrame works with empty list but not empty array #47192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 11, 2022
Merged
10 changes: 7 additions & 3 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ def ndarray_to_mgr(
else:
# by definition an array here
# the dtypes will be coerced to a single dtype
values = _prep_ndarray(values, copy=copy_on_sanitize)
values = _prep_ndarray(values, columns, copy=copy_on_sanitize)
# values = values

if dtype is not None and not is_dtype_equal(values.dtype, dtype):
# GH#40110 see similar check inside sanitize_array
Expand Down Expand Up @@ -537,7 +538,7 @@ def treat_as_nested(data) -> bool:
# ---------------------------------------------------------------------


def _prep_ndarray(values, copy: bool = True) -> np.ndarray:
def _prep_ndarray(values, columns, copy: bool = True) -> np.ndarray:
if isinstance(values, TimedeltaArray) or (
isinstance(values, DatetimeArray) and values.tz is None
):
Expand Down Expand Up @@ -577,7 +578,10 @@ def convert(v):
values = np.array(values, copy=copy)

if values.ndim == 1:
values = values.reshape((values.shape[0], 1))
if len(values) == 0 and columns is not None:
values = values.reshape((0, len(columns)))
else:
values = values.reshape((values.shape[0], 1))
elif values.ndim != 2:
raise ValueError(f"Must pass 2-d input. shape={values.shape}")

Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,6 @@ def test_constructor_2d_index(self):
tm.assert_frame_equal(df, expected)

def test_constructor_error_msgs(self):
msg = "Empty data passed with indices specified."
# passing an empty array with columns specified.
with pytest.raises(ValueError, match=msg):
DataFrame(np.empty(0), columns=list("abc"))

msg = "Mixing dicts with non-Series may lead to ambiguous ordering."
# mix dict and array, wrong size
with pytest.raises(ValueError, match=msg):
Expand Down Expand Up @@ -3086,3 +3081,10 @@ def test_tzaware_data_tznaive_dtype(self, constructor):

assert np.all(result.dtypes == "M8[ns]")
assert np.all(result == ts_naive)

@pytest.mark.parametrize("data", [[], np.array([])])
def test_construction_empty_dataframe(self, data):
# GH#46822
result = DataFrame(data=data, columns=["a", "b"])
assert list(result.values) == []
tm.assert_index_equal(result.columns, Index(["a", "b"]))