From d312a377fc0b48e3e57b045ee95705b3c5715605 Mon Sep 17 00:00:00 2001 From: Jeffrey Tratner Date: Sun, 1 Sep 2013 15:12:12 -0400 Subject: [PATCH] BUG: Fix DataFrame.from_records w/ normal ndarray. Previously, was causing a TypeError about None not being iterable when arr.dtype.names was None. Now goes to 'last ditch' parsing instead. --- doc/source/release.rst | 4 ++-- pandas/core/frame.py | 4 +++- pandas/tests/test_frame.py | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 6530b7e5e9238..abbee59941c8c 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -306,8 +306,8 @@ See :ref:`Internal Refactoring` - Fix boolean comparison with a DataFrame on the lhs, and a list/tuple on the rhs (:issue:`4576`) - Fix error/dtype conversion with setitem of ``None`` on ``Series/DataFrame`` (:issue:`4667`) - Fix decoding based on a passed in non-default encoding in ``pd.read_stata`` (:issue:`4626`) - - Fix some inconsistencies with ``Index.rename`` and ``MultiIndex.rename``, - etc. (:issue:`4718`, :issue:`4628`) + - Fix some inconsistencies with ``Index.rename`` and ``MultiIndex.rename`` (:issue:`4718`, :issue:`4628`) + - Fix ``DataFrame.from_records`` with a plain-vanilla ``ndarray``. (:issue:`4727`) pandas 0.12 =========== diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5d8cc0b9d8de0..4e9f28122b43d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4876,7 +4876,9 @@ def _to_arrays(data, columns, coerce_float=False, dtype=None): return _list_of_series_to_arrays(data, columns, coerce_float=coerce_float, dtype=dtype) - elif isinstance(data, (np.ndarray, Series)): + elif (isinstance(data, (np.ndarray, Series)) + and data.dtype.names is not None): + columns = list(data.dtype.names) arrays = [data[k] for k in columns] return arrays, columns diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 41b705d22b85d..b992b2d60a08d 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -3468,12 +3468,15 @@ def test_from_records_to_records(self): indexed_frame = DataFrame.from_records(arr, index=index) self.assert_(np.array_equal(indexed_frame.index, index)) + # without names, it should go to last ditch + arr2 = np.zeros((2,3)) + tm.assert_frame_equal(DataFrame.from_records(arr2), DataFrame(arr2)) + # wrong length self.assertRaises(Exception, DataFrame.from_records, arr, index=index[:-1]) indexed_frame = DataFrame.from_records(arr, index='f1') - self.assertRaises(Exception, DataFrame.from_records, np.zeros((2, 3))) # what to do? records = indexed_frame.to_records()