Skip to content

Commit 532ed6f

Browse files
authored
The Value Error saying that empty data was passed with indices specif… (#52084)
* The Value Error saying that empty data was passed with indices specified is now only raised when that is indeed the case. * Modified test_constructor_error_msgs to match the changes made in this branch. * Modified the expected message of the test_construction_empty_array_multi_column_raises method in test_constructors.py. * Changed the expected message in test_construction_empty_array_multi_column_raises to a raw string. * Added a note to whatsnew/v2.1.0.rst * 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.
1 parent 5f37c47 commit 532ed6f

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Other enhancements
3535
- Let :meth:`DataFrame.to_feather` accept a non-default :class:`Index` and non-string column names (:issue:`51787`)
3636
- :class:`api.extensions.ExtensionArray` now has a :meth:`~api.extensions.ExtensionArray.map` method (:issue:`51809`)
3737
- Improve error message when having incompatible columns using :meth:`DataFrame.merge` (:issue:`51861`)
38+
- Improved error message when creating a DataFrame with empty data (0 rows), no index and an incorrect number of columns. (:issue:`52084`)
3839

3940
.. ---------------------------------------------------------------------------
4041
.. _whatsnew_210.notable_bug_fixes:

pandas/core/internals/construction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def _check_values_indices_shape_match(
388388
if values.shape[1] != len(columns) or values.shape[0] != len(index):
389389
# Could let this raise in Block constructor, but we get a more
390390
# helpful exception message this way.
391-
if values.shape[0] == 0:
391+
if values.shape[0] == 0 < len(index):
392392
raise ValueError("Empty data passed with indices specified.")
393393

394394
passed = values.shape

pandas/tests/frame/test_constructors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ def test_constructor_error_msgs(self):
656656
msg = "Empty data passed with indices specified."
657657
# passing an empty array with columns specified.
658658
with pytest.raises(ValueError, match=msg):
659-
DataFrame(np.empty(0), columns=list("abc"))
659+
DataFrame(np.empty(0), index=[1])
660660

661661
msg = "Mixing dicts with non-Series may lead to ambiguous ordering."
662662
# mix dict and array, wrong size
@@ -2635,7 +2635,7 @@ def test_from_dict_with_missing_copy_false(self):
26352635

26362636
def test_construction_empty_array_multi_column_raises(self):
26372637
# GH#46822
2638-
msg = "Empty data passed with indices specified."
2638+
msg = r"Shape of passed values is \(0, 1\), indices imply \(0, 2\)"
26392639
with pytest.raises(ValueError, match=msg):
26402640
DataFrame(data=np.array([]), columns=["a", "b"])
26412641

0 commit comments

Comments
 (0)