Skip to content

Commit 514fe2d

Browse files
evanpwjreback
authored andcommitted
BUG: DataFrame constructor fails when columns is set and data=[] (GH9948/9939)
1 parent f1062e6 commit 514fe2d

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/whatsnew/v0.16.1.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ Bug Fixes
230230
- Bug in ``where`` when dtype is ``datetime64/timedelta64``, but dtype of other is not (:issue:`9804`)
231231
- Bug in ``MultiIndex.sortlevel()`` results in unicode level name breaks (:issue:`9856`)
232232
- Bug in which ``groupby.transform`` incorrectly enforced output dtypes to match input dtypes. (:issue:`9807`)
233-
233+
- Bug in ``DataFrame`` constructor when ``columns`` parameter is set, and ``data`` is an empty list (:issue:`9939`)
234234
- Bug in bar plot with ``log=True`` raises ``TypeError`` if all values are less than 1 (:issue:`9905`)
235235
- Bug in horizontal bar plot ignores ``log=True`` (:issue:`9905`)
236236

@@ -249,4 +249,6 @@ Bug Fixes
249249
- Bug causing an exception when setting an empty range using ``DataFrame.loc`` (:issue:`9596`)
250250

251251

252+
253+
252254
- Bug in hiding ticklabels with subplots and shared axes when adding a new plot to an existing grid of axes (:issue:`9158`)

pandas/core/frame.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
265265
mgr = self._init_ndarray(data, index, columns, dtype=dtype,
266266
copy=copy)
267267
else:
268-
mgr = self._init_ndarray(data, index, columns, dtype=dtype,
269-
copy=copy)
268+
mgr = self._init_dict({}, index, columns, dtype=dtype)
270269
elif isinstance(data, collections.Iterator):
271270
raise TypeError("data argument can't be an iterator")
272271
else:

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -3165,6 +3165,19 @@ def test_constructor_empty_list(self):
31653165
expected = DataFrame(index=[])
31663166
assert_frame_equal(df, expected)
31673167

3168+
# GH 9939
3169+
df = DataFrame([], columns=['A', 'B'])
3170+
expected = DataFrame({}, columns=['A', 'B'])
3171+
assert_frame_equal(df, expected)
3172+
3173+
# Empty generator: list(empty_gen()) == []
3174+
def empty_gen():
3175+
return
3176+
yield
3177+
3178+
df = DataFrame(empty_gen(), columns=['A', 'B'])
3179+
assert_frame_equal(df, expected)
3180+
31683181
def test_constructor_list_of_lists(self):
31693182
# GH #484
31703183
l = [[1, 'a'], [2, 'b']]

0 commit comments

Comments
 (0)