diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 51fa0482accca..e15a5de23f79a 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -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: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 449068eff4f43..6c2d4f7919ac6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 8563481c8564d..c945e2560b587 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -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)) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 70e8d3bcf4582..ec18844354f6b 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -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):