Reputation: 23922
Say I have two series: a and b,
a = Series(None, index=['a','b','c'])
b = Series('lol', index=['j','k','l'])
I would like to store b as one of the elements of a,
a['a'] = b
but I get
ValueError: setting an array element with a sequence.
Is it possible to store a pandas series inside a pandas series? How can I do it? Thanks.
Upvotes: 1
Views: 4571
Reputation: 375415
You can recast the dtype
using the method astype
:
In [11]: a = a.astype(object)
In [12]: a['a'] = b
In [13]: a
Out[13]:
a [lol, lol, lol]
b NaN
c NaN
Alternatively (to using astype
) when contructing a
you can force the dtype
to be object:
In [14]: a = Series(None, index=['a','b','c'], dtype=object)
The reason you are getting this error is because float64
, doesn't allow a Series
and similarly it doesn't allow strings - try to set a['a'] = 'lol'
and you'll get a ValueError
.
In [21]: a = Series(None, index=['a','b','c'])
In [22]: a.dtype
Out[22]: dtype('float64')
You can read more about type-casting in the docs.
Upvotes: 6