Skip to content

Commit cec9401

Browse files
committed
BUG: allow DataFrame.from_records to accept empty recarrays
1 parent 84fb0e0 commit cec9401

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pandas 0.11.1
173173
- ``read_html()`` now only allows a single backend: ``html5lib`` (GH3616_)
174174
- ``convert_objects`` with ``convert_dates='coerce'`` was parsing some single-letter strings
175175
into today's date
176+
- ``DataFrame.from_records`` did not accept empty recarrays (GH3682_)
176177

177178
.. _GH3164: https://github.com/pydata/pandas/issues/3164
178179
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -246,6 +247,7 @@ pandas 0.11.1
246247
.. _GH3582: https://github.com/pydata/pandas/issues/3582
247248
.. _GH3676: https://github.com/pydata/pandas/issues/3676
248249
.. _GH3675: https://github.com/pydata/pandas/issues/3675
250+
.. _GH3682: https://github.com/pydata/pandas/issues/3682
249251

250252
pandas 0.11.0
251253
=============

doc/source/v0.11.1.txt

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ Bug Fixes
208208

209209
to replace all occurrences of the string ``'.'`` with ``NaN``.
210210

211+
- ``DataFrame.from_records`` did not accept empty recarrays (GH3682_)
212+
211213
See the `full release notes
212214
<https://github.com/pydata/pandas/blob/master/RELEASE.rst>`__ or issue tracker
213215
on GitHub for a complete list.
@@ -247,3 +249,4 @@ on GitHub for a complete list.
247249
.. _GH3582: https://github.com/pydata/pandas/issues/3582
248250
.. _GH3676: https://github.com/pydata/pandas/issues/3676
249251
.. _GH3675: https://github.com/pydata/pandas/issues/3675
252+
.. _GH3682: https://github.com/pydata/pandas/issues/3682

pandas/core/frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5756,7 +5756,11 @@ def _to_arrays(data, columns, coerce_float=False, dtype=None):
57565756

57575757
return arrays, columns
57585758

5759-
if len(data) == 0:
5759+
if not len(data):
5760+
if isinstance(data, np.ndarray):
5761+
columns = data.dtype.names
5762+
if columns is not None:
5763+
return [[]] * len(columns), columns
57605764
return [], [] # columns if columns is not None else []
57615765
if isinstance(data[0], (list, tuple)):
57625766
return _list_to_arrays(data, columns, coerce_float=coerce_float,

pandas/tests/test_frame.py

+12
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,18 @@ def test_from_records_empty(self):
35223522
expected = DataFrame(columns=['a','b','b'])
35233523
assert_frame_equal(result, expected)
35243524

3525+
def test_from_records_empty_with_nonempty_fields_gh3682(self):
3526+
a = np.array([(1, 2)], dtype=[('id', np.int64), ('value', np.int64)])
3527+
df = DataFrame.from_records(a, index='id')
3528+
assert_array_equal(df.index, Index([1], name='id'))
3529+
self.assertEqual(df.index.name, 'id')
3530+
assert_array_equal(df.columns, Index(['value']))
3531+
3532+
b = np.array([], dtype=[('id', np.int64), ('value', np.int64)])
3533+
df = DataFrame.from_records(b, index='id')
3534+
assert_array_equal(df.index, Index([], name='id'))
3535+
self.assertEqual(df.index.name, 'id')
3536+
35253537
def test_to_records_floats(self):
35263538
df = DataFrame(np.random.rand(10, 10))
35273539
df.to_records()

0 commit comments

Comments
 (0)