0

I have 2 series with different indexes but the same length of 638.

series fc's output is

1888    4.899912
1889    4.900162
1890    4.900141
1891    4.900142
1892    4.900142
          ...   
2521    4.900142
2522    4.900142
2523    4.900142
2524    4.900142
2525    4.900142
Name: predicted_mean, Length: 638, dtype: float64

and series fc_series's output is

Date
2020-12-16   NaN
2020-12-17   NaN
2020-12-18   NaN
2020-12-21   NaN
2020-12-22   NaN
              ..
2023-06-26   NaN
2023-06-27   NaN
2023-06-28   NaN
2023-06-29   NaN
2023-06-30   NaN
Name: predicted_mean, Length: 638, dtype: float64

I tried using this code line

fc_series = pd.Series(fc, index=test_data.index)

I want fc's values in place of the NaN values in fc_series. I am struggling with the indexing part of this and would really appreciate the help.

Aayush Kaushal
  • 101
  • 2
  • 2
  • 10

2 Answers2

2

You were almost there.

The issue is that creating a Series from another Series and passing an Index will perform index alignment, which you precisely don't want here.

Instead, just pass fc's values:

out = pd.Series(fc.to_numpy(), index=fc_series.index)

Or, if you want to modify fc_series in place:

fc_series[:] = fc.to_numpy()

Output:

2020-12-16    4.899912
2020-12-17    4.900162
2020-12-18    4.900141
2020-12-21    4.900142
2020-12-22    4.900142
                ...   
2023-06-26    4.900142
2023-06-27    4.900142
2023-06-28    4.900142
2023-06-29    4.900142
2023-06-30    4.900142
dtype: float64
mozway
  • 194,879
  • 13
  • 39
  • 75
1

Example

import pandas as pd
data1 = {1888: 4.899912, 1889: 4.900162, 1890: 4.900141, 1891: 4.900142, 1892: 4.900142}
s1 = pd.Series(data1)
data2 = {'2020-12-16': float('nan'), '2020-12-17': float('nan'), '2020-12-18': float('nan'), 
         '2020-12-21': float('nan'), '2020-12-22': float('nan')}
s2 = pd.Series(data2)

s1

1888    4.899912
1889    4.900162
1890    4.900141
1891    4.900142
1892    4.900142
dtype: float64

s2

2020-12-16   NaN
2020-12-17   NaN
2020-12-18   NaN
2020-12-21   NaN
2020-12-22   NaN
dtype: float64

Code

If s2 is all NaN, use this code.

s1.set_axis(s2.index)

output:

2020-12-16    4.899912
2020-12-17    4.900162
2020-12-18    4.900141
2020-12-21    4.900142
2020-12-22    4.900142
dtype: float64

If some of the values in s2 are NaN and you want to fill them with the values from s1 only when they are NaN, use this code.

s2.fillna(s1.set_axis(s2.index))
Panda Kim
  • 6,246
  • 2
  • 12