From a28bf57f0f9fa41d0fedb68c26b0072d94c0c47d Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 18 Mar 2024 10:02:17 -0700 Subject: [PATCH 1/3] BUG: Handle Series construction with Dask, dict-like, Series --- pandas/core/series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 699ff413efb91..ab3547b0b4b00 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -543,7 +543,7 @@ def _init_dict( # Looking for NaN in dict doesn't work ({np.nan : 1}[float('nan')] # raises KeyError), so we iterate the entire dict, and align - if data: + if len(data): # GH:34717, issue was using zip to extract key and values from data. # using generators in effects the performance. # Below is the new way of extracting the keys and values From 54b3623f1333445503af001a96ea611f9a61284b Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 18 Mar 2024 10:40:32 -0700 Subject: [PATCH 2/3] Make dict take a different path? --- pandas/core/series.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index ab3547b0b4b00..52f534e0f1346 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -452,7 +452,7 @@ def __init__( data = data.reindex(index) copy = False data = data._mgr - elif is_dict_like(data): + elif isinstance(data, dict): data, index = self._init_dict(data, index, dtype) dtype = None copy = False @@ -519,7 +519,7 @@ def __init__( ) def _init_dict( - self, data, index: Index | None = None, dtype: DtypeObj | None = None + self, data: dict, index: Index | None = None, dtype: DtypeObj | None = None ): """ Derive the "_mgr" and "index" attributes of a new Series from a @@ -543,7 +543,7 @@ def _init_dict( # Looking for NaN in dict doesn't work ({np.nan : 1}[float('nan')] # raises KeyError), so we iterate the entire dict, and align - if len(data): + if data: # GH:34717, issue was using zip to extract key and values from data. # using generators in effects the performance. # Below is the new way of extracting the keys and values From 9f9f50a4dfd2efaac19503246f52220dfc54d57e Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 18 Mar 2024 11:52:56 -0700 Subject: [PATCH 3/3] Use mapping --- pandas/core/series.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 52f534e0f1346..8a7c1531205e0 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -452,7 +452,7 @@ def __init__( data = data.reindex(index) copy = False data = data._mgr - elif isinstance(data, dict): + elif isinstance(data, Mapping): data, index = self._init_dict(data, index, dtype) dtype = None copy = False @@ -519,7 +519,7 @@ def __init__( ) def _init_dict( - self, data: dict, index: Index | None = None, dtype: DtypeObj | None = None + self, data: Mapping, index: Index | None = None, dtype: DtypeObj | None = None ): """ Derive the "_mgr" and "index" attributes of a new Series from a