I have the following pandas data frame:

import numpy as np
import pandas as pd
timestamps = [1, 14, 30]
data = dict(quantities=[1, 4, 9], e_quantities=[1, 2, 3])
df = pd.DataFrame(data=data, columns=data.keys(), index=timestamps)

which looks like this:

    quantities  e_quantities
1            1             1
14           4             2
30           9             3

However, the timestamps should run from 1 to 52:

index = pd.RangeIndex(1, 53)

The following line provides the timestamps that are missing:

series_fill = pd.Series(np.nan, index=index.difference(df.index)).sort_index()

How can I get the quantities and e_quantities columns to have NaN values at these missing timestamps?

I've tried:

df = pd.concat([df, series_fill]).sort_index()

but it adds another column (0) and swaps the order of the original data frame:

     0  e_quantities  quantities
1  NaN           1.0         1.0
2  NaN           NaN         NaN
3  NaN           NaN         NaN

Thanks for any help here.

1 Answers

3
Wen-Ben On Best Solutions

I think you are looking for reindex

df=df.reindex(index)