Skip to content

BUG: Fix DataFrame.from_records w/ normal ndarray. #4727

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
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
4 changes: 2 additions & 2 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ See :ref:`Internal Refactoring<whatsnew_0130.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
===========
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down