Skip to content

The Value Error saying that empty data was passed with indices specif… #52084

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 6 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to have an issue number (even if there's no issue, so here you can put the PR number, i.e. 52084)

Also, I think this would fit better in the "Other enhancements" section, as that's where some other error messages are?

Finally, the note needs to be user-facing and can't mention private methods. How about "Improved error message when creating DataFrame with empty data, no index, and wrong number of columns"?


.. ***DO NOT USE THIS SECTION***

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 @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = 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"])

Expand Down