Skip to content

ENH: Improve error message for empty data constructor. #8020 #12461

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ Other API Changes

- ``.to_latex`` and ``.to_html`` gain a ``decimal`` parameter like ``.to_csv``; the default is ``'.'`` (:issue:`12031`)

- The error message when constructing a DataFrame with empty data and indices specified has been updated.


.. _whatsnew_0180.deprecations:

Expand Down
1 change: 0 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ def _get_axes(N, K, index=index, columns=columns):
values = _prep_ndarray(values, copy=copy)

if dtype is not None:

if values.dtype != dtype:
try:
values = values.astype(dtype)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3875,6 +3875,8 @@ def construction_error(tot_items, block_shape, axes, e=None):
implied = tuple(map(int, [len(ax) for ax in axes]))
if passed == implied and e is not None:
raise e
if block_shape[0] == 0:
raise ValueError("Empty data passed with indices specified.")
raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
passed, implied))

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ def test_constructor_multi_index(self):
self.assertTrue(pd.isnull(df).values.ravel().all())

def test_constructor_error_msgs(self):
msg = "Empty data passed with indices specified."
# passing an empty array with columns specified.
with assertRaisesRegexp(ValueError, msg):
DataFrame(np.empty(0), columns=list('abc'))

msg = "Mixing dicts with non-Series may lead to ambiguous ordering."
# mix dict and array, wrong size
with assertRaisesRegexp(ValueError, msg):
Expand Down