Skip to content

Backport PR #22232 on branch 0.23.x #22244

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.23.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://github.com/pandas-dev/pandas/commit/4efb39f01f5880122fa38d91e12d217ef70fad9e>`_ (:issue:`22227`).
-
-

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)