Skip to content

Commit 1395920

Browse files
mitarrok
authored andcommitted
BUG: Support to create DataFrame from list subclasses.
Issue: pandas-dev#21226
1 parent d43ac97 commit 1395920

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

asv_bench/benchmarks/frame_ctor.py

+13
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,17 @@ def time_frame_from_ndarray(self):
9191
self.df = DataFrame(self.data)
9292

9393

94+
class FromLists(object):
95+
96+
goal_time = 0.2
97+
98+
def setup(self):
99+
N = 1000
100+
M = 100
101+
self.data = [[j for j in range(M)] for i in range(N)]
102+
103+
def time_frame_from_lists(self):
104+
self.df = DataFrame(self.data)
105+
106+
94107
from .pandas_vb_common import setup # noqa: F401

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,7 @@ Other
16061606
- Bug in :meth:`DataFrame.combine_first` in which column types were unexpectedly converted to float (:issue:`20699`)
16071607
- Bug where C variables were declared with external linkage causing import errors if certain other C libraries were imported before Pandas. (:issue:`24113`)
16081608
- Constructing a DataFrame with an index argument that wasn't already an instance of :class:`~pandas.core.Index` was broken in `4efb39f <https://github.com/pandas-dev/pandas/commit/4efb39f01f5880122fa38d91e12d217ef70fad9e>`_ (:issue:`22227`).
1609+
- Bug in :func:`to_object_array` prevented list subclasses to be used to create :class:`DataFrame` (:issue:`21226`)
16091610

16101611
.. _whatsnew_0.24.0.contributors:
16111612

pandas/_libs/lib.pyx

+6-4
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@ def map_infer(ndarray arr, object f, bint convert=1):
22082208
return result
22092209

22102210

2211-
def to_object_array(rows: list, min_width: int=0):
2211+
def to_object_array(rows, int min_width=0):
22122212
"""
22132213
Convert a list of lists into an object array.
22142214
@@ -2229,20 +2229,22 @@ def to_object_array(rows: list, min_width: int=0):
22292229
cdef:
22302230
Py_ssize_t i, j, n, k, tmp
22312231
ndarray[object, ndim=2] result
2232+
list input_rows
22322233
list row
22332234

2234-
n = len(rows)
2235+
input_rows = <list>rows
2236+
n = len(input_rows)
22352237

22362238
k = min_width
22372239
for i in range(n):
2238-
tmp = len(rows[i])
2240+
tmp = len(input_rows[i])
22392241
if tmp > k:
22402242
k = tmp
22412243

22422244
result = np.empty((n, k), dtype=object)
22432245

22442246
for i in range(n):
2245-
row = rows[i]
2247+
row = <list>input_rows[i]
22462248

22472249
for j in range(len(row)):
22482250
result[i, j] = row[j]

pandas/tests/frame/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,15 @@ def test_constructor_range_dtype(self, dtype):
21652165
result = DataFrame({'A': range(5)}, dtype=dtype)
21662166
tm.assert_frame_equal(result, expected)
21672167

2168+
def test_frame_from_list_subclass(self):
2169+
# GH21226
2170+
class List(list):
2171+
pass
2172+
2173+
expected = DataFrame([[1, 2, 3], [4, 5, 6]])
2174+
result = DataFrame(List([List([1, 2, 3]), List([4, 5, 6])]))
2175+
tm.assert_frame_equal(result, expected)
2176+
21682177

21692178
class TestDataFrameConstructorWithDatetimeTZ(TestData):
21702179

0 commit comments

Comments
 (0)