1
            name_col
datetime          
2017-03-22     0.2

I want to add multiple rows till the present date (2017-03-25) so that resulting dataframe looks like:

            name_col
datetime          
2017-03-22     0.2
2017-03-23     0.0
2017-03-24     0.0
2017-03-25     0.0

How do I add multiple rows for each datetime? I can get present date as

from datetime import datetime, timedelta, date
date.today()
2
  • 1
    I'd recommend you to accept @NickilMaveli's solution - it's more idiomatic Commented Mar 25, 2017 at 10:20
  • thanks @MaxU, for your solution and also generosity Commented Mar 25, 2017 at 10:26

2 Answers 2

4

You can also use .resample() method:

In [98]: df
Out[98]:
            name_col
datetime
2017-03-22       0.2

In [99]: df.loc[pd.to_datetime(pd.datetime.now().date())] = 0

In [100]: df
Out[100]:
            name_col
datetime
2017-03-22       0.2
2017-03-25       0.0

In [101]: df.resample('D').bfill()
Out[101]:
            name_col
datetime
2017-03-22       0.2
2017-03-23       0.0
2017-03-24       0.0
2017-03-25       0.0
Sign up to request clarification or add additional context in comments.

Comments

3

You can make use of DF.reindex to align the original DF's indices to a new range of indices generated for an expanded date range.

Note that pd.date_range produces a frequency offset corresponding to daily frequency, and so the need to not specify this explicitly as it's freq parameter.

from datetime import date

df.reindex(pd.date_range(df.index[0], date.today(), name=df.index.name), fill_value=0)

enter image description here

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.