Sam Hatchett
Sam Hatchett

Reputation: 513

reindex Series should fill with NaN

Unless i completely misunderstand what reindex is for, i believe that the snippet below should fill in the series with a NaN for the time index that was removed:

import pandas as pd
import numpy

dateRange = pd.date_range(start='2014-01-01 1:00:00', periods=8, freq='S')
modifiedRange = dateRange.values
modifiedRange = numpy.delete(modifiedRange, (2), axis=0) # remove third row

ts = pd.Series(range(len(modifiedRange)), index=modifiedRange) # series with a gap
print(ts)

ts.reindex(dateRange) # pad the gap with NaN
print(ts)

The output i get is here:

2014-01-01 01:00:00    0
2014-01-01 01:00:01    1
2014-01-01 01:00:03    2
2014-01-01 01:00:04    3
2014-01-01 01:00:05    4
2014-01-01 01:00:06    5
2014-01-01 01:00:07    6
dtype: int64
2014-01-01 01:00:00    0
2014-01-01 01:00:01    1
2014-01-01 01:00:03    2
2014-01-01 01:00:04    3
2014-01-01 01:00:05    4
2014-01-01 01:00:06    5
2014-01-01 01:00:07    6
dtype: int64

The two prints are identical... but I would expect the second to contain the padded NaN value:

2014-01-01 01:00:00    0
2014-01-01 01:00:01    1
2014-01-01 01:00:02    NaN
2014-01-01 01:00:03    2
2014-01-01 01:00:04    3
2014-01-01 01:00:05    4
2014-01-01 01:00:06    5
2014-01-01 01:00:07    6
dtype: int64

Upvotes: 0

Views: 134

Answers (1)

chrisb
chrisb

Reputation: 52266

reindex is not in-place. Do this, and you'll get what you expect.

ts = ts.reindex(dateRange)
print(ts)

Upvotes: 2

Related Questions