From 5362d050fb365d71f96cdcf8fa5a3ba8f83201e2 Mon Sep 17 00:00:00 2001 From: phofl Date: Sun, 24 Jan 2021 14:04:46 +0100 Subject: [PATCH 1/5] DOC: Clarify behavior for Series with dict-like data and index --- 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 f75292f32dbca..306b5299ac19d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -180,8 +180,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame): Values must be hashable and have the same length as `data`. Non-unique index values are allowed. Will default to RangeIndex (0, 1, 2, ..., n) if not provided. If data is dict-like - and index is None, then the values in the index are used to - reindex the Series after it is created using the keys in the data. + and index is None, then the keys in the data are used as index. If the + index is not None, the resulting Series is reindexed with the index values. dtype : str, numpy.dtype, or ExtensionDtype, optional Data type for the output Series. If not specified, this will be inferred from `data`. From 009e0204d2f2882f92e6c0f26afff9ab2590915b Mon Sep 17 00:00:00 2001 From: patrick <61934744+phofl@users.noreply.github.com> Date: Mon, 25 Jan 2021 17:42:59 +0100 Subject: [PATCH 2/5] Update pandas/core/series.py Co-authored-by: Jeff Reback --- 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 306b5299ac19d..3dedf8163da8f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -180,7 +180,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame): Values must be hashable and have the same length as `data`. Non-unique index values are allowed. Will default to RangeIndex (0, 1, 2, ..., n) if not provided. If data is dict-like - and index is None, then the keys in the data are used as index. If the + and index is None, then the keys in the data are used as the index. If the index is not None, the resulting Series is reindexed with the index values. dtype : str, numpy.dtype, or ExtensionDtype, optional Data type for the output Series. If not specified, this will be From 645b0ec9a7b9549021619b6f4406dc9409bc6705 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 5 Feb 2021 00:09:22 +0100 Subject: [PATCH 3/5] Add example --- pandas/core/series.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 3dedf8163da8f..4ce333deb6594 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -190,6 +190,23 @@ class Series(base.IndexOpsMixin, generic.NDFrame): The name to give to the Series. copy : bool, default False Copy input data. + + Examples + -------- + Constructing Series from a dictionary with an Index specified + + >>> d = {'a': 1, 'b': 2, 'c': 3} + >>> ser = pd.Series(data=d, index=['x', 'y', 'z']) + >>> ser + x NaN + y NaN + z NaN + dtype: float64 + + Note that the Index is first build with the keys from the dictionary. + After this the Series is reindexed with the given Index values, hence we + get all NaN as a result. + """ _typ = "series" From ef3db61f37aa1322b0c323c85c8633a078960ba9 Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 6 Feb 2021 01:23:55 +0100 Subject: [PATCH 4/5] Add example with matching index --- pandas/core/series.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 4ce333deb6594..583c8a8e13405 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -195,6 +195,17 @@ class Series(base.IndexOpsMixin, generic.NDFrame): -------- Constructing Series from a dictionary with an Index specified + >>> d = {'a': 1, 'b': 2, 'c': 3} + >>> ser = pd.Series(data=d, index=['a', 'b', 'c']) + >>> ser + a 1 + b 2 + c 3 + dtype: int64 + + The keys of the dictionary match with the Index values, hence the Index + values have no effect. + >>> d = {'a': 1, 'b': 2, 'c': 3} >>> ser = pd.Series(data=d, index=['x', 'y', 'z']) >>> ser From d6802cd4e6408c56d1022e78e8d76c8be8275c3c Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 6 Feb 2021 01:25:24 +0100 Subject: [PATCH 5/5] Fix ci checks error --- pandas/core/series.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 583c8a8e13405..593e97515771e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -217,7 +217,6 @@ class Series(base.IndexOpsMixin, generic.NDFrame): Note that the Index is first build with the keys from the dictionary. After this the Series is reindexed with the given Index values, hence we get all NaN as a result. - """ _typ = "series"