Skip to content

Commit b46b3ef

Browse files
committed
BUG: DataFrame.from_records with empty rec array
keep the dtypes Closes pandas-dev#20805
1 parent 41db527 commit b46b3ef

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ Numeric
10921092
- 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`)
10931093
- 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`)
10941094
- Bug in the :class:`DataFrame` constructor in which data containing very large positive or very large negative numbers was causing ``OverflowError`` (:issue:`18584`)
1095+
- Bug in the :meth:`DataFrame.from_records` constructor losing the dtypes of a empty NumPy record array (:issue:`20805`)
10951096
- Bug in :class:`Index` constructor with ``dtype='uint64'`` where int-like floats were not coerced to :class:`UInt64Index` (:issue:`18400`)
10961097
- 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`)
10971098
- Multiplication and division of numeric-dtyped :class:`Index` objects with timedelta-like scalars returns ``TimedeltaIndex`` instead of raising ``TypeError`` (:issue:`19333`)

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -7387,7 +7387,10 @@ def _to_arrays(data, columns, coerce_float=False, dtype=None):
73877387
if isinstance(data, np.ndarray):
73887388
columns = data.dtype.names
73897389
if columns is not None:
7390-
return [[]] * len(columns), columns
7390+
arrays = [np.array([], dtype=dtype)
7391+
for _, dtype in data.dtype.descr]
7392+
return arrays, columns
7393+
73917394
return [], [] # columns if columns is not None else []
73927395
if isinstance(data[0], (list, tuple)):
73937396
return _list_to_arrays(data, columns, coerce_float=coerce_float,

pandas/tests/frame/test_constructors.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1933,9 +1933,18 @@ def test_from_records_empty_with_nonempty_fields_gh3682(self):
19331933

19341934
b = np.array([], dtype=[('id', np.int64), ('value', np.int64)])
19351935
df = DataFrame.from_records(b, index='id')
1936-
tm.assert_index_equal(df.index, Index([], name='id'))
1936+
tm.assert_index_equal(df.index, Index([], name='id', dtype='int'))
19371937
assert df.index.name == 'id'
19381938

1939+
def test_from_records_empty_dtypes(self):
1940+
# https://github.com/pandas-dev/pandas/issues/20805
1941+
a = np.array([(1, 2)], dtype=[('id', 'u8'), ('value', 'i8')])
1942+
result = DataFrame.from_records(a[:0])
1943+
expected = pd.DataFrame({"id": np.array([], dtype='u8'),
1944+
"value": np.array([], dtype='i8')},
1945+
columns=['id', 'value'])
1946+
tm.assert_frame_equal(result, expected)
1947+
19391948
def test_from_records_with_datetimes(self):
19401949

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

0 commit comments

Comments
 (0)