Skip to content

Commit c81d03b

Browse files
jylinjreback
authored andcommitted
ERR: Improve error message for empty dataframe constructor.
closes #8020 closes #12461
1 parent 1cd87c9 commit c81d03b

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

doc/source/whatsnew/v0.18.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,8 @@ Other API Changes
869869

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

872+
- More helpful error message when constructing a ``DataFrame`` with empty data but with indices (:issue:`8020`)
873+
872874

873875
.. _whatsnew_0180.deprecations:
874876

pandas/core/frame.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ def _get_axes(N, K, index=index, columns=columns):
411411
values = _prep_ndarray(values, copy=copy)
412412

413413
if dtype is not None:
414-
415414
if values.dtype != dtype:
416415
try:
417416
values = values.astype(dtype)

pandas/core/internals.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,6 +3875,8 @@ def construction_error(tot_items, block_shape, axes, e=None):
38753875
implied = tuple(map(int, [len(ax) for ax in axes]))
38763876
if passed == implied and e is not None:
38773877
raise e
3878+
if block_shape[0] == 0:
3879+
raise ValueError("Empty data passed with indices specified.")
38783880
raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
38793881
passed, implied))
38803882

pandas/tests/frame/test_constructors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ def test_constructor_multi_index(self):
285285
self.assertTrue(pd.isnull(df).values.ravel().all())
286286

287287
def test_constructor_error_msgs(self):
288+
msg = "Empty data passed with indices specified."
289+
# passing an empty array with columns specified.
290+
with assertRaisesRegexp(ValueError, msg):
291+
DataFrame(np.empty(0), columns=list('abc'))
292+
288293
msg = "Mixing dicts with non-Series may lead to ambiguous ordering."
289294
# mix dict and array, wrong size
290295
with assertRaisesRegexp(ValueError, msg):

0 commit comments

Comments
 (0)