diff --git a/doc/source/whatsnew/v0.23.5.txt b/doc/source/whatsnew/v0.23.5.txt index ee0ee4259f86d..916a246355b5f 100644 --- a/doc/source/whatsnew/v0.23.5.txt +++ b/doc/source/whatsnew/v0.23.5.txt @@ -20,6 +20,9 @@ and bug fixes. We recommend that all users upgrade to this version. Fixed Regressions ~~~~~~~~~~~~~~~~~ +- Constructing a DataFrame with an index argument that wasn't already an + instance of :class:`~pandas.core.Index` was broken in `4efb39f + `_ (:issue:`22227`). - - diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2a40dd28a6fd7..54d4006c78de4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7354,6 +7354,8 @@ def _arrays_to_mgr(arrays, arr_names, index, columns, dtype=None): # figure out the index, if necessary if index is None: index = extract_index(arrays) + else: + index = ensure_index(index) # don't force copy because getting jammed in an ndarray anyway arrays = _homogenize(arrays, index, dtype) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 70dd358248bc4..849e617c47824 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2230,3 +2230,16 @@ def test_frame_timeseries_column(self): Timestamp('20130101T10:01:00', tz='US/Eastern'), Timestamp('20130101T10:02:00', tz='US/Eastern')]}) tm.assert_frame_equal(result, expected) + + def test_nested_dict_construction(self): + # GH22227 + columns = ['Nevada', 'Ohio'] + pop = {'Nevada': {2001: 2.4, 2002: 2.9}, + 'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}} + result = pd.DataFrame(pop, index=[2001, 2002, 2003], columns=columns) + expected = pd.DataFrame( + [(2.4, 1.7), (2.9, 3.6), (np.nan, np.nan)], + columns=columns, + index=pd.Index([2001, 2002, 2003]) + ) + tm.assert_frame_equal(result, expected)