Background
I have a numpy.ndarray of shape==(95,15). I already have the desired Series.Index names, of len(my_index)==95. I want to create a Series in which every index is associated with one of the rows of my 95x15 numpy.ndarray.
Variable Names
pfit: 95x15numpy.ndarraymy_index: 95x1list(str)
Steps Taken
- The following fails with corresponding error:
my_series = pd.Series(index=my_index, dtype="object", data=pfit)
Traceback (most recent call last):
File "C:\Users\gford1\AppData\Local\Temp\1/ipykernel_22244/2329315457.py", line 1, in <module>
my_series = pd.Series(index=my_index, dtype="object", data=pfit)
File "C:\Users\gford1\AppData\Local\Programs\Spyder\pkgs\pandas\core\series.py", line 439, in __init__
data = sanitize_array(data, index, dtype, copy)
File "C:\Users\gford1\AppData\Local\Programs\Spyder\pkgs\pandas\core\construction.py", line 577, in sanitize_array
subarr = _sanitize_ndim(subarr, data, dtype, index, allow_2d=allow_2d)
File "C:\Users\gford1\AppData\Local\Programs\Spyder\pkgs\pandas\core\construction.py", line 628, in _sanitize_ndim
raise ValueError("Data must be 1-dimensional")
ValueError: Data must be 1-dimensional
- I therefore have to iterate through
my_indexand add thepfitarrays, one-by-one:
my_series = pd.Series(index=my_index, dtype="object")
i = 0
for idx in my_series.index:
my_series[idx] = pfit[i]
i+=1
#2 works, but I believe that there is a better / faster way that I am unaware of.
data=list(pfit))help?Seriesis, or what your iteration does. Look atmy_series.values. Has that "preserved" the array?list(pfit)), to then only again retrieve it as a Numpy array when I need it.