nostradukemas
nostradukemas

Reputation: 317

Adding only Business Days to Date column in DataFrame

I have a DataFrame I need to append the previous business day's values to.

Date        Col1  Col2
2019-10-17  7     5
2019-10-18  3     4
2019-10-21  2     1

I've decided to build a new DataFrame with the row of new data, append it to the big DataFrame, and use that as the new DataFrame.

I want the date to autogenerate by using the date of the previous business day. So far, I've tried the following, but can't figure out how to get it to use Friday's date on a Monday.

import pandas as pd
from datetime import datetime, timedelta

yesterday = (datetime.now() - timedelta(days=1))

d = {'Date': [yesterday], 'Col1:': [Col1data], 'Col2:': [Col2data]}
df = pd.DataFrame(data=d)

Is there a way to get the timedelta to only look at weekdays, or some other method?

Upvotes: 1

Views: 758

Answers (1)

Suraj Motaparthy
Suraj Motaparthy

Reputation: 520

from pandas.tseries.offsets import BDay
yesterday = (datetime.now() - BDay(1))

Upvotes: 2

Related Questions