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?

Share:
35,888
deubNippon
Author by

deubNippon

Updated on March 07, 2020

Comments

  • deubNippon
    deubNippon about 4 years

    I would like to convert a list of pandas.Series into a numpy array of pandas.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
    deubNippon about 10 years
    Thanks 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
    jme about 10 years
    Don'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
    deubNippon about 10 years
    Well..thanks for trying, but it's not working indeed. Unfortunately, I really need a np.array
  • MANU
    MANU over 6 years
    If the aim is to convert to Numpy-array representation the you can see pandas.Series.as_matrix or pandas.DataFrame.values