Reputation: 1568
I got a pandas Series object with an index starting from '2020-01-01 01:00:00+00:00'
till 2020-12-19
. I would like to add the index '2020-01-01 00:00:00+00:00'
with the value np.nan
.
data_dict[i]
date_time
2020-01-01 01:00:00+00:00 13
2020-01-01 02:00:00+00:00 13
2020-01-01 03:00:00+00:00 13
2020-01-01 04:00:00+00:00 13
2020-01-01 05:00:00+00:00 13
...
2020-12-18 20:00:00+00:00 25
2020-12-18 21:00:00+00:00 25
2020-12-18 22:00:00+00:00 25
2020-12-18 23:00:00+00:00 25
2020-12-19 00:00:00+00:00 20
When I use:
nan = pd.Series([np.nan], index=['2020-01-01 00:00:00+00:00'])
data_dict[i].append(nan)
data_dict[i].sort_index()
seems like nothing happens?
data_dict[i]
date_time
2020-01-01 01:00:00+00:00 13
2020-01-01 02:00:00+00:00 13
2020-01-01 03:00:00+00:00 13
2020-01-01 04:00:00+00:00 13
2020-01-01 05:00:00+00:00 13
...
2020-12-18 21:00:00+00:00 25
2020-12-18 22:00:00+00:00 25
2020-12-18 23:00:00+00:00 25
2020-12-19 00:00:00+00:00 20
How would I add it at the right place(ie: at the beginning of the series object)
Upvotes: 1
Views: 1273
Reputation: 862661
If use .append
new data are added to end of Series
, for correct order sorting values of DatetimeIndex
:
s = s1.append(s2).sort_index()
If need to append to start of Series
, swap order - add second Series
to first one:
s = s2.append(s1)
EDIT: Here is necessary assign back Series.append
and create DatetimeIndex
in added Series:
nan = pd.Series([np.nan], index=pd.to_datetime(['2020-01-01 00:00:00+00:00']))
data_dict[i] = data_dict[i].append(nan)
data_dict[i] = data_dict[i].sort_index()
Upvotes: 3