Skip to content

BUG: DataFrame.from_records with empty rec array #20806

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ Numeric
- Bug in :class:`Series` constructor with an int or float list where specifying ``dtype=str``, ``dtype='str'`` or ``dtype='U'`` failed to convert the data elements to strings (:issue:`16605`)
- Bug in :class:`Index` multiplication and division methods where operating with a ``Series`` would return an ``Index`` object instead of a ``Series`` object (:issue:`19042`)
- Bug in the :class:`DataFrame` constructor in which data containing very large positive or very large negative numbers was causing ``OverflowError`` (:issue:`18584`)
- Bug in the :meth:`DataFrame.from_records` constructor losing the dtypes of a empty NumPy record array (:issue:`20805`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to 0.23.1

- Bug in :class:`Index` constructor with ``dtype='uint64'`` where int-like floats were not coerced to :class:`UInt64Index` (:issue:`18400`)
- Bug in :class:`DataFrame` flex arithmetic (e.g. ``df.add(other, fill_value=foo)``) with a ``fill_value`` other than ``None`` failed to raise ``NotImplementedError`` in corner cases where either the frame or ``other`` has length zero (:issue:`19522`)
- Multiplication and division of numeric-dtyped :class:`Index` objects with timedelta-like scalars returns ``TimedeltaIndex`` instead of raising ``TypeError`` (:issue:`19333`)
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7387,7 +7387,10 @@ def _to_arrays(data, columns, coerce_float=False, dtype=None):
if isinstance(data, np.ndarray):
columns = data.dtype.names
if columns is not None:
return [[]] * len(columns), columns
arrays = [np.array([], dtype=dtype)
for _, dtype in data.dtype.descr]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for using the descr here instead of just the dtype? ref #20734 and the docs for dtype.descrI think we may run into some obscure issues using the str representation of the type instead of the type directly

return arrays, columns

return [], [] # columns if columns is not None else []
if isinstance(data[0], (list, tuple)):
return _list_to_arrays(data, columns, coerce_float=coerce_float,
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,9 +1933,18 @@ def test_from_records_empty_with_nonempty_fields_gh3682(self):

b = np.array([], dtype=[('id', np.int64), ('value', np.int64)])
df = DataFrame.from_records(b, index='id')
tm.assert_index_equal(df.index, Index([], name='id'))
tm.assert_index_equal(df.index, Index([], name='id', dtype='int'))
assert df.index.name == 'id'

def test_from_records_empty_dtypes(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we should cover more dtypes? Perhaps a good use case for a shared fixture?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some more here?

# https://github.com/pandas-dev/pandas/issues/20805
a = np.array([(1, 2)], dtype=[('id', 'u8'), ('value', 'i8')])
result = DataFrame.from_records(a[:0])
expected = pd.DataFrame({"id": np.array([], dtype='u8'),
"value": np.array([], dtype='i8')},
columns=['id', 'value'])
tm.assert_frame_equal(result, expected)

def test_from_records_with_datetimes(self):

# this may fail on certain platforms because of a numpy issue
Expand Down