Wolverine
Wolverine

Reputation: 51

get first business day of last month in panda DataFrame column

My DataSet ranges from 1989-1-03 to present date. It looks something like this. I have Column R which has Dates at certain points which matches the Index of the DataFrame.

 Index     R
1989-01-24      NaT

1989-01-25      NaT

1989-01-26      NaT

1989-01-27      NaT

1989-01-30      NaT

1989-01-31      NaT

1989-02-01     1989-02-01 

1989-02-02      NaT

1989-02-03      NaT

I want find rows from Column R which are not NaT, subtract one month from them and find the value of the first business day for that month and Store into a new column RI in the DataFrame.

The Final Data Frame should look something like this:

Index       R           RI

1989-01-02   NaT       1989-01-02

----

1989-02-01  1989-02-01      NaT 

1989-02-02    NaT           NaT

1989-02-03    NaT           NaT

Can someone help me ? I am really weak at DateTime handling.

Upvotes: 1

Views: 1784

Answers (1)

Phil Cooper
Phil Cooper

Reputation: 5877

You specifically asked about a pandas dataframe so you should look into pandas Business Date Range functions.

One such solution would be:

import pandas as pd   
pd.bdate_range(start='2017-07-01',periods=1)[0]
# returns Timestamp('2017-07-03 00:00:00', freq='B')

Upvotes: 1

Related Questions