Skip to content

Commit 867aea2

Browse files
DOC/TST: DataFrame constructor with a list of DataFrames (#34991)
* DOC/TST: DataFrame constructor with a list of DataFrames Closes #32289
1 parent 02ab42f commit 867aea2

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ Other API changes
696696
- :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`)
697697
- :class:`Period` no longer accepts tuples for the ``freq`` argument (:issue:`34658`)
698698
- :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`)
699+
- 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`).
699700

700701

701702
Increased minimum versions for dependencies

pandas/core/internals/construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def convert(v):
321321
if values.ndim == 1:
322322
values = values.reshape((values.shape[0], 1))
323323
elif values.ndim != 2:
324-
raise ValueError("Must pass 2-d input")
324+
raise ValueError(f"Must pass 2-d input. shape={values.shape}")
325325

326326
return values
327327

pandas/tests/frame/test_constructors.py

+14-24
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytz
1212

1313
from pandas.compat import PY37, is_platform_little_endian
14-
from pandas.compat.numpy import _is_numpy_dev
14+
from pandas.compat.numpy import _np_version_under1p19
1515

1616
from pandas.core.dtypes.common import is_integer_dtype
1717

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

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

156-
result = DataFrame([DataFrame(dict(A=np.arange(5)))])
157-
assert isinstance(result.iloc[0, 0], DataFrame)
158+
with pytest.raises(ValueError, match=r"shape=\(1, 0, 0\)"):
159+
pd.DataFrame([b])
160+
161+
a = pd.DataFrame({"A": [1, 2]})
162+
with pytest.raises(ValueError, match=r"shape=\(2, 2, 1\)"):
163+
pd.DataFrame([a, a])
158164

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

510-
@pytest.mark.xfail(_is_numpy_dev, reason="Interprets embedded frame as 3D")
511-
def test_constructor_with_embedded_frames(self):
512-
513-
# embedded data frames
514-
df1 = DataFrame({"a": [1, 2, 3], "b": [3, 4, 5]})
515-
df2 = DataFrame([df1, df1 + 10])
516-
517-
df2.dtypes
518-
str(df2)
519-
520-
result = df2.loc[0, 0]
521-
tm.assert_frame_equal(result, df1)
522-
523-
result = df2.loc[1, 0]
524-
tm.assert_frame_equal(result, df1 + 10)
525-
526516
def test_constructor_subclass_dict(self, float_frame, dict_subclass):
527517
# Test for passing dict subclass to constructor
528518
data = {

0 commit comments

Comments
 (0)