Skip to content

DOC/TST: DataFrame constructor with a list of DataFrames #34991

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ Other API changes
- :func: `merge` now checks ``suffixes`` parameter type to be ``tuple`` and raises ``TypeError``, whereas before a ``list`` or ``set`` were accepted and that the ``set`` could produce unexpected results (:issue:`33740`)
- :class:`Period` no longer accepts tuples for the ``freq`` argument (:issue:`34658`)
- :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` now raises ValueError if ``limit_direction`` is 'forward' or 'both' and ``method`` is 'backfill' or 'bfill' or ``limit_direction`` is 'backward' or 'both' and ``method`` is 'pad' or 'ffill' (:issue:`34746`)
- The :class:`DataFrame` constructor no longer accepts a list of ``DataFrame`` objects. Because of changes to NumPy, ``DataFrame`` objects are now consistently treated as 2D objects, so a list of ``DataFrames`` is considered 3D, and no longer acceptible for the ``DataFrame`` constructor (:issue:`32289`).


Increased minimum versions for dependencies
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def convert(v):
if values.ndim == 1:
values = values.reshape((values.shape[0], 1))
elif values.ndim != 2:
raise ValueError("Must pass 2-d input")
raise ValueError(f"Must pass 2-d input. shape={values.shape}")

return values

Expand Down
38 changes: 14 additions & 24 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytz

from pandas.compat import PY37, is_platform_little_endian
from pandas.compat.numpy import _is_numpy_dev
from pandas.compat.numpy import _np_version_under1p19

from pandas.core.dtypes.common import is_integer_dtype

Expand Down Expand Up @@ -147,14 +147,20 @@ def test_constructor_dtype_list_data(self):
assert df.loc[1, 0] is None
assert df.loc[0, 1] == "2"

@pytest.mark.xfail(_is_numpy_dev, reason="Interprets list of frame as 3D")
def test_constructor_list_frames(self):
# see gh-3243
result = DataFrame([DataFrame()])
assert result.shape == (1, 0)
@pytest.mark.skipif(_np_version_under1p19, reason="NumPy change.")
def test_constructor_list_of_2d_raises(self):
# https://github.com/pandas-dev/pandas/issues/32289
a = pd.DataFrame()
b = np.empty((0, 0))
with pytest.raises(ValueError, match=r"shape=\(1, 0, 0\)"):
pd.DataFrame([a])

result = DataFrame([DataFrame(dict(A=np.arange(5)))])
assert isinstance(result.iloc[0, 0], DataFrame)
with pytest.raises(ValueError, match=r"shape=\(1, 0, 0\)"):
pd.DataFrame([b])

a = pd.DataFrame({"A": [1, 2]})
with pytest.raises(ValueError, match=r"shape=\(2, 2, 1\)"):
pd.DataFrame([a, a])

def test_constructor_mixed_dtypes(self):
def _make_mixed_dtypes_df(typ, ad=None):
Expand Down Expand Up @@ -507,22 +513,6 @@ def test_constructor_error_msgs(self):
with pytest.raises(ValueError, match=msg):
DataFrame({"a": False, "b": True})

@pytest.mark.xfail(_is_numpy_dev, reason="Interprets embedded frame as 3D")
def test_constructor_with_embedded_frames(self):

# embedded data frames
df1 = DataFrame({"a": [1, 2, 3], "b": [3, 4, 5]})
df2 = DataFrame([df1, df1 + 10])

df2.dtypes
str(df2)

result = df2.loc[0, 0]
tm.assert_frame_equal(result, df1)

result = df2.loc[1, 0]
tm.assert_frame_equal(result, df1 + 10)

def test_constructor_subclass_dict(self, float_frame, dict_subclass):
# Test for passing dict subclass to constructor
data = {
Expand Down