Skip to content

Commit 5aa62dc

Browse files
cpcloudMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #22232: BUG: Fix DataFrame construction regression
1 parent c420e75 commit 5aa62dc

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

doc/source/whatsnew/v0.23.5.txt

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ and bug fixes. We recommend that all users upgrade to this version.
2020
Fixed Regressions
2121
~~~~~~~~~~~~~~~~~
2222

23+
- Constructing a DataFrame with an index argument that wasn't already an
24+
instance of :class:`~pandas.core.Index` was broken in `4efb39f
25+
<https://github.com/pandas-dev/pandas/commit/4efb39f01f5880122fa38d91e12d217ef70fad9e>`_ (:issue:`22227`).
2326
-
2427
-
2528

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -7354,6 +7354,8 @@ def _arrays_to_mgr(arrays, arr_names, index, columns, dtype=None):
73547354
# figure out the index, if necessary
73557355
if index is None:
73567356
index = extract_index(arrays)
7357+
else:
7358+
index = ensure_index(index)
73577359

73587360
# don't force copy because getting jammed in an ndarray anyway
73597361
arrays = _homogenize(arrays, index, dtype)

pandas/tests/frame/test_constructors.py

+13
Original file line numberDiff line numberDiff line change
@@ -2230,3 +2230,16 @@ def test_frame_timeseries_column(self):
22302230
Timestamp('20130101T10:01:00', tz='US/Eastern'),
22312231
Timestamp('20130101T10:02:00', tz='US/Eastern')]})
22322232
tm.assert_frame_equal(result, expected)
2233+
2234+
def test_nested_dict_construction(self):
2235+
# GH22227
2236+
columns = ['Nevada', 'Ohio']
2237+
pop = {'Nevada': {2001: 2.4, 2002: 2.9},
2238+
'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
2239+
result = pd.DataFrame(pop, index=[2001, 2002, 2003], columns=columns)
2240+
expected = pd.DataFrame(
2241+
[(2.4, 1.7), (2.9, 3.6), (np.nan, np.nan)],
2242+
columns=columns,
2243+
index=pd.Index([2001, 2002, 2003])
2244+
)
2245+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)