Skip to content
Open
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Bug fixes
- Fixed :class:`ArrowDtype` string arithmetic for addition between ``string`` and ``large_string`` typed arrays (:issue:`65220`)
- Fixed a bug in adding missing values (e.g. :attr:`NA`) to a pyarrow-backed string array or Series
raising an error instead of propagating the missing value (:issue:`64968`)
- Fixed a bug in :meth:`DataFrame.from_dict` where rows corresponding to an empty :class:`Series` or ``dict`` are dropped when ``orient='index'`` (:issue:`62775`)
- Fixed a bug in :meth:`Series.rank` with period dtype and missing values, always sorting missing values at the top, regardless of the ``na_option`` value (:issue:`64976`)
- Fixed a bug in reading files with a chained URL scheme (e.g. ``tar://``) with the latest version of ``fsspec`` (:issue:`65462`).
- Fixed a hang in :meth:`Series.str.replace` for PyArrow-backed string data when replacing an empty pattern; behavior now matches Python's ``str.replace`` (:issue:`64941`).
Expand Down
15 changes: 11 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -19011,13 +19011,20 @@ def values(self) -> np.ndarray:


def _from_nested_dict(
data: Mapping[HashableT, Mapping[HashableT2, T]],
) -> collections.defaultdict[HashableT2, dict[HashableT, T]]:
new_data: collections.defaultdict[HashableT2, dict[HashableT, T]] = (
data: Mapping[HashableT, Mapping[HashableT2, T | None]],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe with my suggestion, all of these code changes are no longer necessary and can be reverted. Can you confirm.

) -> collections.defaultdict[HashableT2, dict[HashableT, T | None]]:
new_data: collections.defaultdict[HashableT2, dict[HashableT, T | None]] = (
collections.defaultdict(dict)
)
cols = []
for s in data.values():
for col in s.keys():
if col not in cols:
cols.append(col)

for index, s in data.items():
for col, v in s.items():
for col in cols:
v = s.get(col, None)
new_data[col][index] = v
return new_data

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/constructors/test_from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def test_constructor_single_row(self):
).reindex(result.index)
tm.assert_frame_equal(result, expected)

def test_from_nested_dict_series_with_empty_series(self):
data = {"good": Series({"a": 1, "b": 2}), "blank": Series()}

result = DataFrame.from_dict(data, orient="index")
expected = DataFrame(
{
"a": {"good": 1.0, "blank": np.nan},
"b": {"good": 2.0, "blank": np.nan},
}
)
tm.assert_frame_equal(result, expected)

def test_constructor_list_of_series(self):
data = [
OrderedDict([["a", 1.5], ["b", 3.0], ["c", 4.0]]),
Expand Down
Loading