hlin117
hlin117

Reputation: 22268

Python Pandas: Making date index continuous

I have the following time series data:

           EventCount
Date
2015-06-23          1    
2015-06-25          1
2015-05-29          1

I need to pad the data, so the index has continuous dates, like this:

           EventCount
Date
2015-06-23          1
2015-06-24          0
2015-06-25          1
2015-06-26          0
2015-06-27          0
2015-06-28          0
2015-05-29          1

How could I do this?

Upvotes: 2

Views: 2638

Answers (1)

Jianxun Li
Jianxun Li

Reputation: 24742

You can use the .reindex().

# your data
# ===================================
print(df)

            EventCount
Date                  
2015-06-23           1
2015-06-25           1
2015-06-29           1

# processing
# ==============================
df.reindex(pd.date_range(df.index[0], df.index[-1], freq='D')).fillna(0)

            EventCount
2015-06-23           1
2015-06-24           0
2015-06-25           1
2015-06-26           0
2015-06-27           0
2015-06-28           0
2015-06-29           1

Upvotes: 8

Related Questions