0

shalom !

I want to fill the missing values of a pandas series with nan, in the series bellow the 6.0 is missing, how can i use that pandas series bellow to obtain an np.array or other series like without having to use a for look (i need to repeat this operation a lot of times, a loop will took too much time)

input : 
1    2.0
2    3.0
3    4.0
4    5.0
5    7.0
6    8.0
7    9.0

return : 
np.array([2.0,3.0,4.0,5.0,np.nan,7.0,8.0,9.0])

i recieved response with the wrong inputs (wrong index situation basically) so be careful with the index there.

thank you in advance

i know how to solve it with a loop, but it isn't what i am looking for here

maroke
  • 1
  • 1

1 Answers1

0

You can use reindex:

s = pd.Series(dict(zip([1,2,4],[1,1,1])))
array = s.reindex(np.arange(s.index[0], s.index[-1]+1), fill_value=np.nan).values
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • well for my problems, the index won't be 1,2,4 , but 1,2,3 and i know it miss a nan value between the index 2 and 3 – maroke May 23 '23 at 07:50