Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Add 1 every month in dataframe for all columns

I have a dataframe:

            A    B   C
date
2021-01-01  1    nan 1
2021-01-23  nan  1   1
2021-02-03  1    nan 1

How can I add "1" to all columns at the beginning of each month? (Note I also want to do this quarterly as well) The dataframe should end up looking like this:

            A   B   C
date
2021-01-01  2   nan 2
2021-01-23  nan 1   1
2021-02-01  nan 1   1
2021-02-03  1   nan 1

The beginning of the month should have "nan" in the same place as the last instance of the previous month.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

IIUC the logic, you could do:

# ensure datetime
df.index = pd.to_datetime(df.index)

# fill missing starts of month
idx = pd.date_range(df.index.min(), df.index.max(), freq='MS')
df = df.reindex(df.index.union(idx))

# update starts of month
prev = df.shift(1).loc[idx] # get last data of previous month
df.loc[idx] = df.loc[idx].add(1).combine_first(prev) # increment/fill

output:

              A    B    C
2021-01-01  2.0  NaN  2.0
2021-01-23  NaN  1.0  1.0
2021-02-01  NaN  1.0  1.0
2021-02-03  1.0  NaN  1.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading