-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reasoning for using the |
||
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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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