Skip to content

Commit 75f9bb8

Browse files
Backport PR pandas-dev#39374: DOC: Clarify behavior for Series with dict-like data and index (pandas-dev#39645)
Co-authored-by: patrick <[email protected]>
1 parent 3946cd4 commit 75f9bb8

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

pandas/core/series.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
169169
Values must be hashable and have the same length as `data`.
170170
Non-unique index values are allowed. Will default to
171171
RangeIndex (0, 1, 2, ..., n) if not provided. If data is dict-like
172-
and index is None, then the values in the index are used to
173-
reindex the Series after it is created using the keys in the data.
172+
and index is None, then the keys in the data are used as the index. If the
173+
index is not None, the resulting Series is reindexed with the index values.
174174
dtype : str, numpy.dtype, or ExtensionDtype, optional
175175
Data type for the output Series. If not specified, this will be
176176
inferred from `data`.
@@ -179,6 +179,33 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
179179
The name to give to the Series.
180180
copy : bool, default False
181181
Copy input data.
182+
183+
Examples
184+
--------
185+
Constructing Series from a dictionary with an Index specified
186+
187+
>>> d = {'a': 1, 'b': 2, 'c': 3}
188+
>>> ser = pd.Series(data=d, index=['a', 'b', 'c'])
189+
>>> ser
190+
a 1
191+
b 2
192+
c 3
193+
dtype: int64
194+
195+
The keys of the dictionary match with the Index values, hence the Index
196+
values have no effect.
197+
198+
>>> d = {'a': 1, 'b': 2, 'c': 3}
199+
>>> ser = pd.Series(data=d, index=['x', 'y', 'z'])
200+
>>> ser
201+
x NaN
202+
y NaN
203+
z NaN
204+
dtype: float64
205+
206+
Note that the Index is first build with the keys from the dictionary.
207+
After this the Series is reindexed with the given Index values, hence we
208+
get all NaN as a result.
182209
"""
183210

184211
_typ = "series"

0 commit comments

Comments
 (0)