From 030f90a87d64126933fe68aaee21d75b75f1a05c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20M=C5=B1ller?= Date: Mon, 20 Mar 2023 14:00:59 +0100 Subject: [PATCH 1/6] The Value Error saying that empty data was passed with indices specified is now only raised when that is indeed the case. --- pandas/core/internals/construction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index b114b8a1aa7aa..e660abafcd567 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -382,7 +382,7 @@ def _check_values_indices_shape_match( if values.shape[1] != len(columns) or values.shape[0] != len(index): # Could let this raise in Block constructor, but we get a more # helpful exception message this way. - if values.shape[0] == 0: + if values.shape[0] == 0 < len(index): raise ValueError("Empty data passed with indices specified.") passed = values.shape From cd4ef3323c6da03a7faac37d7c803435fe9374e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20M=C5=B1ller?= Date: Mon, 20 Mar 2023 15:56:50 +0100 Subject: [PATCH 2/6] Modified test_constructor_error_msgs to match the changes made in this branch. --- pandas/tests/frame/test_constructors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index db7e97d672b4d..cf5abd7338ab2 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -656,7 +656,7 @@ 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")) + DataFrame(np.empty(0), index=[1]) msg = "Mixing dicts with non-Series may lead to ambiguous ordering." # mix dict and array, wrong size From f14b44eb74097d0b0981a1033bd8ba463b7614ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20M=C5=B1ller?= Date: Mon, 20 Mar 2023 16:06:04 +0100 Subject: [PATCH 3/6] Modified the expected message of the test_construction_empty_array_multi_column_raises method in test_constructors.py. --- pandas/tests/frame/test_constructors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index cf5abd7338ab2..6056769b60fcc 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2635,7 +2635,7 @@ def test_from_dict_with_missing_copy_false(self): def test_construction_empty_array_multi_column_raises(self): # GH#46822 - msg = "Empty data passed with indices specified." + msg = "Shape of passed values is \(0, 1\), indices imply \(0, 2\)" with pytest.raises(ValueError, match=msg): DataFrame(data=np.array([]), columns=["a", "b"]) From 4513cfef40fe909e4bdc703b6606831d0fdc8dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20M=C5=B1ller?= Date: Mon, 20 Mar 2023 16:35:03 +0100 Subject: [PATCH 4/6] Changed the expected message in test_construction_empty_array_multi_column_raises to a raw string. --- pandas/tests/frame/test_constructors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 6056769b60fcc..cb61a68200411 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2635,7 +2635,7 @@ def test_from_dict_with_missing_copy_false(self): def test_construction_empty_array_multi_column_raises(self): # GH#46822 - msg = "Shape of passed values is \(0, 1\), indices imply \(0, 2\)" + msg = r"Shape of passed values is \(0, 1\), indices imply \(0, 2\)" with pytest.raises(ValueError, match=msg): DataFrame(data=np.array([]), columns=["a", "b"]) From 31de34ce40b09d9b1d161b05b089a7b01215fddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20M=C5=B1ller?= Date: Mon, 20 Mar 2023 17:39:34 +0100 Subject: [PATCH 5/6] Added a note to whatsnew/v2.1.0.rst --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index aff30d113438e..748f2eb8f0ec2 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -241,6 +241,7 @@ Styler Other ^^^^^ - Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) +- Bug in :func:`_check_values_indices_shape_match` no longer raises ValueError with a message saying that indices were specified when creating an empty DataFrame that has an issue with the number of columns in the data and in the columns argument. .. ***DO NOT USE THIS SECTION*** From 507dec5f04f756ff25dc469e36647c02778f9831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20M=C5=B1ller?= Date: Mon, 20 Mar 2023 19:38:05 +0100 Subject: [PATCH 6/6] Moved error message to other enhancements, updated it so it describes the visible behavior for the library user instead of the behavior of a private method. --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 748f2eb8f0ec2..8e506cd9b8cbe 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -35,6 +35,7 @@ Other enhancements - Let :meth:`DataFrame.to_feather` accept a non-default :class:`Index` and non-string column names (:issue:`51787`) - :class:`api.extensions.ExtensionArray` now has a :meth:`~api.extensions.ExtensionArray.map` method (:issue:`51809`) - Improve error message when having incompatible columns using :meth:`DataFrame.merge` (:issue:`51861`) +- Improved error message when creating a DataFrame with empty data (0 rows), no index and an incorrect number of columns. (:issue:`52084`) .. --------------------------------------------------------------------------- .. _whatsnew_210.notable_bug_fixes: @@ -241,7 +242,6 @@ Styler Other ^^^^^ - Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) -- Bug in :func:`_check_values_indices_shape_match` no longer raises ValueError with a message saying that indices were specified when creating an empty DataFrame that has an issue with the number of columns in the data and in the columns argument. .. ***DO NOT USE THIS SECTION***