Skip to content

BUG: Support to create DataFrame from list subclasses #21238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions asv_bench/benchmarks/frame_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,17 @@ def time_frame_from_ndarray(self):
self.df = DataFrame(self.data)


class FromLists(object):

goal_time = 0.2

def setup(self):
N = 1000
M = 100
self.data = [[j for j in range(M)] for i in range(N)]

def time_frame_from_lists(self):
self.df = DataFrame(self.data)


from .pandas_vb_common import setup # noqa: F401
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,7 @@ Other
- Bug in :meth:`DataFrame.combine_first` in which column types were unexpectedly converted to float (:issue:`20699`)
- Bug where C variables were declared with external linkage causing import errors if certain other C libraries were imported before Pandas. (:issue:`24113`)
- 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`).
- Bug in :func:`to_object_array` prevented list subclasses to be used to create :class:`DataFrame` (:issue:`21226`)

.. _whatsnew_0.24.0.contributors:

Expand Down
10 changes: 6 additions & 4 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2208,7 +2208,7 @@ def map_infer(ndarray arr, object f, bint convert=1):
return result


def to_object_array(rows: list, min_width: int=0):
def to_object_array(rows: object, int min_width=0):
"""
Convert a list of lists into an object array.

Expand All @@ -2229,20 +2229,22 @@ def to_object_array(rows: list, min_width: int=0):
cdef:
Py_ssize_t i, j, n, k, tmp
ndarray[object, ndim=2] result
list input_rows
list row

n = len(rows)
input_rows = <list>rows
n = len(input_rows)

k = min_width
for i in range(n):
tmp = len(rows[i])
tmp = len(input_rows[i])
if tmp > k:
k = tmp

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

for i in range(n):
row = rows[i]
row = <list>input_rows[i]

for j in range(len(row)):
result[i, j] = row[j]
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,15 @@ def test_constructor_range_dtype(self, dtype):
result = DataFrame({'A': range(5)}, dtype=dtype)
tm.assert_frame_equal(result, expected)

def test_frame_from_list_subclass(self):
# GH21226
class List(list):
pass

expected = DataFrame([[1, 2, 3], [4, 5, 6]])
result = DataFrame(List([List([1, 2, 3]), List([4, 5, 6])]))
tm.assert_frame_equal(result, expected)


class TestDataFrameConstructorWithDatetimeTZ(TestData):

Expand Down