Skip to content

Commit 3c4ecb7

Browse files
cpcloudjreback
authored andcommitted
BUG: Fix DataFrame construction regression (#22232)
1 parent 81f386c commit 3c4ecb7

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
@@ -7606,6 +7606,8 @@ def _arrays_to_mgr(arrays, arr_names, index, columns, dtype=None):
76067606
# figure out the index, if necessary
76077607
if index is None:
76087608
index = extract_index(arrays)
7609+
else:
7610+
index = ensure_index(index)
76097611

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

pandas/tests/frame/test_constructors.py

+13
Original file line numberDiff line numberDiff line change
@@ -2240,3 +2240,16 @@ def test_frame_timeseries_column(self):
22402240
Timestamp('20130101T10:01:00', tz='US/Eastern'),
22412241
Timestamp('20130101T10:02:00', tz='US/Eastern')]})
22422242
tm.assert_frame_equal(result, expected)
2243+
2244+
def test_nested_dict_construction(self):
2245+
# GH22227
2246+
columns = ['Nevada', 'Ohio']
2247+
pop = {'Nevada': {2001: 2.4, 2002: 2.9},
2248+
'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
2249+
result = pd.DataFrame(pop, index=[2001, 2002, 2003], columns=columns)
2250+
expected = pd.DataFrame(
2251+
[(2.4, 1.7), (2.9, 3.6), (np.nan, np.nan)],
2252+
columns=columns,
2253+
index=pd.Index([2001, 2002, 2003])
2254+
)
2255+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)