Skip to content

Commit 7543426

Browse files
author
Vibavari Gurunathan
authored
BUG: Fix from_records() column reorder issue, if columns!=None use passed param (#59717) (#59809)
* BUG: Fix columns param reorder issue - if columns!=None, use passed param (#59717) * Add tests for to_arrays() * Fix import order with isort * fix sort * Update datatype to int32 * Fis test * Revert commit * Add test for DaaFrame.from_records() * Apply comments * Delete test_to_arrays.py
1 parent 4b22453 commit 7543426

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ I/O
619619
^^^
620620
- Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`)
621621
- Bug in :meth:`.DataFrame.to_json` when ``"index"`` was a value in the :attr:`DataFrame.column` and :attr:`Index.name` was ``None``. Now, this will fail with a ``ValueError`` (:issue:`58925`)
622+
- Bug in :meth:`DataFrame.from_records` where ``columns`` parameter with numpy structured array was not reordering and filtering out the columns (:issue:`59717`)
622623
- Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`)
623624
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)
624625
- Bug in :meth:`DataFrame.to_stata` when writing :class:`DataFrame` and ``byteorder=`big```. (:issue:`58969`)

pandas/core/internals/construction.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ def to_arrays(
750750

751751
elif isinstance(data, np.ndarray) and data.dtype.names is not None:
752752
# e.g. recarray
753-
columns = Index(list(data.dtype.names))
753+
if columns is None:
754+
columns = Index(data.dtype.names)
754755
arrays = [data[k] for k in columns]
755756
return arrays, columns
756757

pandas/tests/frame/constructors/test_from_records.py

+23
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,26 @@ def test_from_records_empty2(self):
469469

470470
alt = DataFrame(arr)
471471
tm.assert_frame_equal(alt, expected)
472+
473+
def test_from_records_structured_array(self):
474+
# GH 59717
475+
data = np.array(
476+
[
477+
("John", 25, "New York", 50000),
478+
("Jane", 30, "San Francisco", 75000),
479+
("Bob", 35, "Chicago", 65000),
480+
("Alice", 28, "Los Angeles", 60000),
481+
],
482+
dtype=[("name", "U10"), ("age", "i4"), ("city", "U15"), ("salary", "i4")],
483+
)
484+
485+
actual_result = DataFrame.from_records(data, columns=["name", "salary", "city"])
486+
487+
modified_data = {
488+
"name": ["John", "Jane", "Bob", "Alice"],
489+
"salary": np.array([50000, 75000, 65000, 60000], dtype="int32"),
490+
"city": ["New York", "San Francisco", "Chicago", "Los Angeles"],
491+
}
492+
expected_result = DataFrame(modified_data)
493+
494+
tm.assert_frame_equal(actual_result, expected_result)

0 commit comments

Comments
 (0)