0

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 Answer 1

3

I think you are looking for reindex

df=df.reindex(index)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.