From d2d680d5aa7286083f12dbebd92fbdd1547509f0 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 25 Jun 2020 08:46:43 -0500 Subject: [PATCH 1/2] DOC/TST: DataFrame constructor with a list of DataFrames Closes #32289 --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/core/internals/construction.py | 2 +- pandas/tests/frame/test_constructors.py | 38 +++++++++---------------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 0d2254e401103..ba7d1c3486a80 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -684,6 +684,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 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 diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index d49f1f154a2c1..4b9db810dead0 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -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 diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 02a871666c78d..05fdc470f27bd 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -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_under1p20 from pandas.core.dtypes.common import is_integer_dtype @@ -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_under1p20, 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): @@ -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 = { From 5df8cba56d40fd0177f6a2d466c7a9ba307086c1 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 25 Jun 2020 15:56:14 -0500 Subject: [PATCH 2/2] skip for 1.19 --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/tests/frame/test_constructors.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ba7d1c3486a80..6c65602118182 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -684,7 +684,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 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`). +- 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 diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 05fdc470f27bd..dba243f1a339a 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -11,7 +11,7 @@ import pytz from pandas.compat import PY37, is_platform_little_endian -from pandas.compat.numpy import _np_version_under1p20 +from pandas.compat.numpy import _np_version_under1p19 from pandas.core.dtypes.common import is_integer_dtype @@ -147,7 +147,7 @@ def test_constructor_dtype_list_data(self): assert df.loc[1, 0] is None assert df.loc[0, 1] == "2" - @pytest.mark.skipif(_np_version_under1p20, reason="NumPy change.") + @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()