Skip to content

Commit 2f00c7f

Browse files
committed
Convert tuple to list before _list_to_arrays when construct DataFrame.
Signed-off-by: HE, Tao <[email protected]>
1 parent a5d251d commit 2f00c7f

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Bug Fixes
124124
~~~~~~~~~
125125
- Bug in :func:`to_datetime` which would raise an (incorrect) ``ValueError`` when called with a date far into the future and the ``format`` argument specified instead of raising ``OutOfBoundsDatetime`` (:issue:`23830`)
126126
- Bug in an error message in :meth:`DataFrame.plot`. Improved the error message if non-numerics are passed to :meth:`DataFrame.plot` (:issue:`25481`)
127-
-
127+
- Segmentation fault when construct :class:`DataFrame` from non-empty tuples (:issue:`25691`)
128128

129129
Categorical
130130
^^^^^^^^^^^

pandas/core/internals/construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def to_arrays(data, columns, coerce_float=False, dtype=None):
394394
return [[]] * len(columns), columns
395395
return [], [] # columns if columns is not None else []
396396
if isinstance(data[0], (list, tuple)):
397-
return _list_to_arrays(data, columns, coerce_float=coerce_float,
397+
return _list_to_arrays(list(data), columns, coerce_float=coerce_float,
398398
dtype=dtype)
399399
elif isinstance(data[0], compat.Mapping):
400400
return _list_of_dict_to_arrays(data, columns,

pandas/tests/frame/test_constructors.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1202,12 +1202,26 @@ def test_constructor_mixed_type_rows(self):
12021202
expected = DataFrame([[1, 2], [3, 4]])
12031203
tm.assert_frame_equal(result, expected)
12041204

1205-
def test_constructor_tuples(self):
1205+
@pytest.mark.parametrize("tuples,lists", [
1206+
((), []),
1207+
((()), []),
1208+
(((), ()), [(), ()]),
1209+
(((), ()), [[], []]),
1210+
(([], []), [[], []]),
1211+
(([1, 2, 3], [4, 5, 6]), [[1, 2, 3], [4, 5, 6]])
1212+
])
1213+
def test_constructor_tuple(self, tuples, lists):
1214+
# GH 25691
1215+
result = DataFrame(tuples)
1216+
expected = DataFrame(lists)
1217+
tm.assert_frame_equal(result, expected)
1218+
1219+
def test_constructor_list_of_tuples(self):
12061220
result = DataFrame({'A': [(1, 2), (3, 4)]})
12071221
expected = DataFrame({'A': Series([(1, 2), (3, 4)])})
12081222
tm.assert_frame_equal(result, expected)
12091223

1210-
def test_constructor_namedtuples(self):
1224+
def test_constructor_list_of_namedtuples(self):
12111225
# GH11181
12121226
from collections import namedtuple
12131227
named_tuple = namedtuple("Pandas", list('ab'))

0 commit comments

Comments
 (0)