import pandas as pd
pd.Series(data={"a":[1,2,3,3,5]}, index=[1,2,3,4,5])
Problem description
If I use the code above, the output is like this.
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
dtype: float64
According to the document,it says data could be dict, but actually, if I use dict, the data in Series object will be NaN.
I don't know why ? Is the way I proposed above to create a series is illegal?
Comment From: TomAugspurger
See the second section here about from a dictionary. The keys are interpreted as the index, and the values are the values of the Series
, so you get a Series
with a single row, A
, and then reindex it. Essentially:
In [3]: pd.Series([[1, 2, 3, 3, 5]], index=['A'])
Out[3]:
A [1, 2, 3, 3, 5]
dtype: object
In [4]: pd.Series([[1, 2, 3, 3, 5]], index=['A']).reindex([1, 2, 3, 4, 5])
Out[4]:
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
dtype: object
The docstring could probably use some examples, but we have other issues for that so I'm going to close this one.