Pb converting a list of pandas.Series into a numpy array of pandas.Series
35,888
You should set the dtype
of the array when you assign it:
l = [pd.Series([1,2,3]),pd.Series([4,5,6])]
np.array(l, dtype=pd.Series)
Though it is raises the question: why do you want an ndarray of series, and not an ndarray of the contents of the series?
Author by
deubNippon
Updated on March 07, 2020Comments
-
deubNippon about 4 years
I would like to convert a list of
pandas.Series
into a numpy array ofpandas.Series
. But when I call the array constructor, it also converting my Series.>>> l = [Series([1,2,3]),Series([4,5,6])] >>> np.array(l) array([[1, 2, 3], [4, 5, 6]], dtype=int64)
My list is small (~10 elements), so for performances issues I would like to avoid to create a pandas.DataFrame. Is there an easy workaround?
Thanks in advance
-
deubNippon about 10 yearsThanks a lot! I need to use later on,on some of those Series, a rolling_moment function, which to my knowledge, exist only in Pandas.
-
jme about 10 yearsDon't thank me too fast! On second inspection, it may not work. But you might want to just keep the series objects in a python list, in that case.
-
deubNippon about 10 yearsWell..thanks for trying, but it's not working indeed. Unfortunately, I really need a np.array
-
MANU over 6 yearsIf the aim is to convert to Numpy-array representation the you can see pandas.Series.as_matrix or pandas.DataFrame.values