Skip to content

Commit ff254f7

Browse files
committed
ENH: handle tuple-like objects as last ditch effort in DataFrame.from_records, close pandas-dev#907
1 parent 55942c7 commit ff254f7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pandas/core/frame.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4220,8 +4220,10 @@ def _to_sdict(data, columns, coerce_float=False):
42204220
elif isinstance(data[0], Series):
42214221
return _list_of_series_to_sdict(data, columns,
42224222
coerce_float=coerce_float)
4223-
else: # pragma: no cover
4224-
raise TypeError('No logic to handle %s type' % type(data[0]))
4223+
else:
4224+
# last ditch effort
4225+
data = map(tuple, data)
4226+
return _list_to_sdict(data, columns, coerce_float=coerce_float)
42254227

42264228
def _list_to_sdict(data, columns, coerce_float=False):
42274229
if len(data) > 0 and isinstance(data[0], tuple):

pandas/tests/test_frame.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,25 @@ def test_from_records_bad_index_column(self):
18811881
self.assertRaises(Exception, DataFrame.from_records, df, index=[2])
18821882
self.assertRaises(KeyError, DataFrame.from_records, df, index=2)
18831883

1884+
def test_from_records_non_tuple(self):
1885+
class Record(object):
1886+
1887+
def __init__(self, *args):
1888+
self.args = args
1889+
1890+
def __getitem__(self, i):
1891+
return self.args[i]
1892+
1893+
def __iter__(self):
1894+
return iter(self.args)
1895+
1896+
recs = [Record(1, 2, 3), Record(4, 5, 6), Record(7, 8, 9)]
1897+
tups = map(tuple, recs)
1898+
1899+
result = DataFrame.from_records(recs)
1900+
expected = DataFrame.from_records(tups)
1901+
assert_frame_equal(result, expected)
1902+
18841903
def test_get_agg_axis(self):
18851904
cols = self.frame._get_agg_axis(0)
18861905
self.assert_(cols is self.frame.columns)

0 commit comments

Comments
 (0)