Skip to content

Commit 965bfa1

Browse files
authored
DOC: Clarify behavior for Series with dict-like data and index (#39374)
1 parent 95ee0b6 commit 965bfa1

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
@@ -180,8 +180,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
180180
Values must be hashable and have the same length as `data`.
181181
Non-unique index values are allowed. Will default to
182182
RangeIndex (0, 1, 2, ..., n) if not provided. If data is dict-like
183-
and index is None, then the values in the index are used to
184-
reindex the Series after it is created using the keys in the data.
183+
and index is None, then the keys in the data are used as the index. If the
184+
index is not None, the resulting Series is reindexed with the index values.
185185
dtype : str, numpy.dtype, or ExtensionDtype, optional
186186
Data type for the output Series. If not specified, this will be
187187
inferred from `data`.
@@ -190,6 +190,33 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
190190
The name to give to the Series.
191191
copy : bool, default False
192192
Copy input data.
193+
194+
Examples
195+
--------
196+
Constructing Series from a dictionary with an Index specified
197+
198+
>>> d = {'a': 1, 'b': 2, 'c': 3}
199+
>>> ser = pd.Series(data=d, index=['a', 'b', 'c'])
200+
>>> ser
201+
a 1
202+
b 2
203+
c 3
204+
dtype: int64
205+
206+
The keys of the dictionary match with the Index values, hence the Index
207+
values have no effect.
208+
209+
>>> d = {'a': 1, 'b': 2, 'c': 3}
210+
>>> ser = pd.Series(data=d, index=['x', 'y', 'z'])
211+
>>> ser
212+
x NaN
213+
y NaN
214+
z NaN
215+
dtype: float64
216+
217+
Note that the Index is first build with the keys from the dictionary.
218+
After this the Series is reindexed with the given Index values, hence we
219+
get all NaN as a result.
193220
"""
194221

195222
_typ = "series"

0 commit comments

Comments
 (0)