I want to add a column called 'Date' which starts from todays date and adds business days as you go down the df up until a year. I am trying the below code but it repeats days as its adding a BD to Friday and Saturdays. The output should have row 1 = 2021-10-07 and end with 2022-10-08 with only BD being shown. Can anyone help please?
import datetime as dt
from pandas.tseries.offsets import BDay
from datetime import date
df = pd.DataFrame({'Date': pd.date_range(start=date.today(), end=date.today() + dt.timedelta(days=365))})
df['Date'] = df['Date'] + BDay(1)
It is unclear what your desired output is, but if you want a column 'Date' that only shows the dates for business days, you can use the code below.
import datetime as dt
import pandas as pd
from datetime import date
df = pd.DataFrame({'Date': pd.date_range(start=date.today(), end=date.today() + dt.timedelta(days=365))})
df = df[df.Date.dt.weekday < 5] # 0 is Monday, # 6 is Sunday
Related
I have a column with the weekday, another with the month and another with the year. How do I get the actual date in python?
import pandas as pd
df = pd.DataFrame({"year": [2018], "month": [12], "day": [1]})
df["date"] = pd.to_datetime(df[["year", "month", "day"]]).dt.date
print(df)
# year month day date
# 0 2018 12 1 2018-12-01
You can use the datetime library:
import datetime
x = datetime.datetime(year, month, day)
print(x)
Documentation: https://docs.python.org/3/library/datetime.html
from datetime import date
print(date.today())
This should work!
I am working on a project and I am trying to calculate the number of business days within a month. What I currently did was extract all of the unique months from one dataframe into a different dataframe and created a second column with
df2['Signin Date Shifted'] = df2['Signin Date'] + pd.DateOffset(months=1)
Thus the current dataframe looks like:
I know I can do dt.daysinmonth or a timedelta but that gives me all of the days within a month including Sundays/Saturdays (which I don't want).
Using busday_count from np
Ex:
import pandas as pd
import numpy as np
df = pd.DataFrame({"Signin Date": ["2018-01-01", "2018-02-01"]})
df["Signin Date"] = pd.to_datetime(df["Signin Date"])
df['Signin Date Shifted'] = pd.DatetimeIndex(df['Signin Date']) + pd.DateOffset(months=1)
df["bussDays"] = np.busday_count( df["Signin Date"].values.astype('datetime64[D]'), df['Signin Date Shifted'].values.astype('datetime64[D]'))
print(df)
Output:
Signin Date Signin Date Shifted bussDays
0 2018-01-01 2018-02-01 23
1 2018-02-01 2018-03-01 20
MoreInfo
I have a daily data dataframe (df) indexed by date - the head is below:
nominal
date
2016-01-04 114185.481138
2016-01-04 17841.990960
2016-01-04 -8799.514730
2016-01-04 0.000000
2016-01-04 -3028.765682
I can find the max date using
maxDate = df.index.max()
How would I find the date 260 working days (1 working year) before this date? How could I go about retrieving the date 260 days ago from the maxDate?
By using Bday
from pandas.tseries.offsets import BDay
df.index.max()-BDay(260)
Timestamp('2015-01-05 00:00:00')
If I understanding you wanting to subtract a date, in your case it would be like this:
import datetime
dat = datetime.datetie(2016,1,4)
dd = datetime.timedelta(days = 260)
print(dat - dd)
output: 2015-04-19
import datetime
#Only use the following line if the column type for your 'date' column is
# string
df['date'] = pd.to_datetime(df['date'])
(max(df['date']) - pd.tseries.offsets.BDay(260)).strftime('%Y-%m-%d')
#The line above produces:
# '2015-01-06'
I have a data frame that contains 2 columns, one is Date and other is float number.
I would like to add those 2 to get the following:
Index Date Days NewDate
0 20-04-2016 5 25-04-2016
1 16-03-2015 3.7 20-03-2015
As you can see if there is decimal it is converted as int as 3.1--> 4 (days).
I have some weird questions so I appreciate any help.
Thank you !
First, ensure that the Date column is a datetime object:
df['Date'] = pd.to_datetime(df['Date'])
Then, we can convert the Days column to int by ceiling it and the converting it to a pandas Timedelta:
temp = df['Days'].apply(np.ceil).apply(lambda x: pd.Timedelta(x, unit='D'))
Datetime objects and timedeltas can be added:
df['NewDate'] = df['Date'] + temp
You can convert the Days column to timedelta and add it to Date column:
import pandas as pd
df['NewDate'] = pd.to_datetime(df.Date) + pd.to_timedelta(pd.np.ceil(df.Days), unit="D")
df
using combine for two columns calculations and pd.DateOffset for adding days
df['NewDate'] = df['Date'].combine(df['Days'], lambda x,y: x + pd.DateOffset(days=int(np.ceil(y))))
output:
Date Days NewDate
0 2016-04-20 5.0 2016-04-25
1 2016-03-16 3.7 2016-03-20
Have a dateframe like that:
Trying to change '2001-01-01' value in column to date (function of today's date). But this one approach does not work:
date = dt.date.today()
df.loc[df['dat_csz_opzione_tech'] == '2001-01-01', 'dat_csz_opzione_tech'] = date
How can I do this?
Try this
import pandas as pd
import time
df = pd.DataFrame({ 'dat_csz_opzione_tech' :['2001-02-01','2001-01-01','2001-03-01','2001-04-01']})
todaysdate = time.strftime("%Y-%m-%d")
df.loc[df['dat_csz_opzione_tech'] == '2001-01-01', 'dat_csz_opzione_tech'] = todaysdate
print df
Output
dat_csz_opzione_tech
0 2001-02-01
1 2017-02-14
2 2001-03-01
3 2001-04-01