When we want to reindex an existing Series using TimedeltaIndex/PeriodIndex/DatetimeIndex, the result will have np.NaN as data
In [1]: import pandas as pd
In [2]: # Case of `TimedeltaIndex`
In [3]: a = pd.Series(['A'] * 3)
In [4]: t_d_idx = pd.to_timedelta([0, 10, 20], unit='s')
In [5]: type(t_d_idx)
Out[5]: pandas.tseries.tdi.TimedeltaIndex
In [6]: a
Out[6]:
0 A
1 A
2 A
dtype: object
In [7]: a.reindex(index=t_d_idx)
Out[7]:
00:00:00 NaN
00:00:10 NaN
00:00:20 NaN
dtype: object
In [8]: # Case of `PeriodIndex`
In [9]: b = pd.Series(['B'] * 3)
In [10]: p_idx = pd.period_range('2010-01', '2010-03', freq='M')
In [11]: type(p_idx)
Out[11]: pandas.tseries.period.PeriodIndex
In [12]: b
Out[12]:
0 B
1 B
2 B
dtype: object
In [13]: b.reindex(index=p_idx)
Out[13]:
2010-01 NaN
2010-02 NaN
2010-03 NaN
Freq: M, dtype: object
In [14]: # Case of `DatetimeIndex`
In [15]: c = pd.Series(['C'] * 3)
In [16]: d_t_idx = pd.DatetimeIndex(start='2016-1-1', end='2016-4-1', freq='M')
In [17]: type(d_t_idx)
Out[17]: pandas.tseries.index.DatetimeIndex
In [18]: c
Out[18]:
0 C
1 C
2 C
dtype: object
In [19]: c.reindex(index=d_t_idx)
Out[19]:
2016-01-31 NaN
2016-02-29 NaN
2016-03-31 NaN
Freq: M, dtype: object
Even when you want to reindex a Series which already have DatetimeIndex as index
In [20]: ts = pd.Series(
....: range(3),
....: index=pd.DatetimeIndex(
....: start='2016-2-1',
....: end='2016-2-22',
....: freq='W'
....: )
....: )
In [21]: type(ts.index)
Out[21]: pandas.tseries.index.DatetimeIndex
In [22]: ts
Out[22]:
2016-02-07 0
2016-02-14 1
2016-02-21 2
Freq: W-SUN, dtype: int64
In [23]: ts.reindex(index=d_t_idx)
Out[23]:
2016-01-31 NaN
2016-02-29 NaN
2016-03-31 NaN
Freq: M, dtype: float64
Is this what we expect Series.index should behave?
Comment From: jorisvandenbossche
This is exactly the expected behaviour of reindex
. If you want to set a new index, you can do:
In [26]: c.index = d_t_idx
In [27]: c
Out[27]:
2016-01-31 C
2016-02-29 C
2016-03-31 C
Freq: M, dtype: object
reindex
is meant to set the index but match the existing index/data combinations to the new index. Since your original index and the new indexes you want to reindex with have nothing in common, you get all NaN values.