Pandas series - more elements in index


#1

Hi, In the “pandas – Data Structures” course, below statements looks wrong.

‘pd.Series(d, index=[‘b’, ‘c’, ‘d’, ‘a’]) – If there are more elements in the index, then corresponding value will be NaN’

when i tried running below code it gives me exception - ‘ValueError: Wrong number of items passed 5, placement implies 6’

import pandas as pd
import numpy as np

se = pd.Series(np.random.randn(5), index=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’])
print(se)


#2

Hi!

I tried running the same program, and I got the ValueError:

I am running Python 3.x and Pandas 0.12.0

But, when you do same with dataframe you get NaN.

This page might help you: https://pandas.pydata.org/pandas-docs/stable/dsintro.html


#3

Hi,

yes, thats correct. I also noticed that data frame works fine with mismatch but with series also i expected NaN instead it throws type error.

Thanks for the link. I found that this behavior is different for ndarray, dict and scalar values. For dict, it does show NaN if indexes are more. Also, in case of dict, index overwrite the keys. Whereas in case of ndarray or scalar values, it throws type error if number of index and value mismatch

dict

s = pd.Series({‘a’ : 1, ‘b’ : 2}, index=[‘g’, ‘b’, ‘c’, ‘d’, ‘e’,‘f’])
s
g NaN
b 2.0
c NaN
d NaN
e NaN
f NaN

ndarray

s = pd.Series(np.random.randn(5), index=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’,‘f’])
Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python2.7/site-packages/pandas/core/series.py”, line 266, in init
data = SingleBlockManager(data, index, fastpath=True)
File “/usr/lib/python2.7/site-packages/pandas/core/internals.py”, line 4397, in init
fastpath=True)
File “/usr/lib/python2.7/site-packages/pandas/core/internals.py”, line 2952, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File “/usr/lib/python2.7/site-packages/pandas/core/internals.py”, line 120, in init
len(self.mgr_locs)))
ValueError: Wrong number of items passed 5, placement implies 6

scalar

pd.Series([5 ,3 , 4 , 6, 7], index=[‘a’, ‘b’, ‘c’, ‘d’, ‘e’,‘f’])
Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python2.7/site-packages/pandas/core/series.py”, line 266, in init
data = SingleBlockManager(data, index, fastpath=True)
File “/usr/lib/python2.7/site-packages/pandas/core/internals.py”, line 4397, in init
fastpath=True)
File “/usr/lib/python2.7/site-packages/pandas/core/internals.py”, line 2952, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File “/usr/lib/python2.7/site-packages/pandas/core/internals.py”, line 120, in init
len(self.mgr_locs)))
ValueError: Wrong number of items passed 5, placement implies 6