File tree 3 files changed +22
-1
lines changed
3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ Fixed regressions
16
16
~~~~~~~~~~~~~~~~~
17
17
- Performance regression in :meth: `DataFrame.isin ` and :meth: `Series.isin ` for nullable data types (:issue: `42714 `)
18
18
- Regression in updating values of :class: `pandas.Series ` using boolean index, created by using :meth: `pandas.DataFrame.pop ` (:issue: `42530 `)
19
- -
19
+ - Regression in :meth: ` DataFrame.from_records ` with empty records ( :issue: ` 42456 `)
20
20
-
21
21
22
22
.. ---------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -757,6 +757,14 @@ def to_arrays(
757
757
# i.e. numpy structured array
758
758
columns = ensure_index (data .dtype .names )
759
759
arrays = [data [name ] for name in columns ]
760
+
761
+ if len (data ) == 0 :
762
+ # GH#42456 the indexing above results in list of 2D ndarrays
763
+ # TODO: is that an issue with numpy?
764
+ for i , arr in enumerate (arrays ):
765
+ if arr .ndim == 2 :
766
+ arrays [i ] = arr [:, 0 ]
767
+
760
768
return arrays , columns
761
769
return [], ensure_index ([])
762
770
Original file line number Diff line number Diff line change @@ -457,3 +457,16 @@ def test_from_records_empty_with_nonempty_fields_gh3682(self):
457
457
b = a [:0 ]
458
458
df2 = DataFrame .from_records (b , index = "id" )
459
459
tm .assert_frame_equal (df2 , df .iloc [:0 ])
460
+
461
+ def test_from_records_empty2 (self ):
462
+ # GH#42456
463
+ dtype = [("prop" , int )]
464
+ shape = (0 , len (dtype ))
465
+ arr = np .empty (shape , dtype = dtype )
466
+
467
+ result = DataFrame .from_records (arr )
468
+ expected = DataFrame ({"prop" : np .array ([], dtype = int )})
469
+ tm .assert_frame_equal (result , expected )
470
+
471
+ alt = DataFrame (arr )
472
+ tm .assert_frame_equal (alt , expected )
You can’t perform that action at this time.
0 commit comments