diff --git a/doc/source/whatsnew/v3.0.3.rst b/doc/source/whatsnew/v3.0.3.rst index 71d9111247cdf..e08ece6dae4e5 100644 --- a/doc/source/whatsnew/v3.0.3.rst +++ b/doc/source/whatsnew/v3.0.3.rst @@ -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`). diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 51471c74009e6..d2fa9f5b686ee 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -19014,7 +19014,7 @@ def values(self) -> np.ndarray: def _from_nested_dict( - data: Mapping[HashableT, Mapping[HashableT2, T]], + data: Mapping[HashableT, Mapping[HashableT2, T | None]], ) -> collections.defaultdict[HashableT2, dict[HashableT, T]]: new_data: collections.defaultdict[HashableT2, dict[HashableT, T]] = ( collections.defaultdict(dict) diff --git a/pandas/tests/frame/constructors/test_from_dict.py b/pandas/tests/frame/constructors/test_from_dict.py index c94965d0e2f21..cd5cd2a4b03e5 100644 --- a/pandas/tests/frame/constructors/test_from_dict.py +++ b/pandas/tests/frame/constructors/test_from_dict.py @@ -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]]),