I have in a dataframe:
Customer ID,
Customer date of birth,
date of purchase.
I need a function to calculate the distance in +/- days between the date of birth and the date of purchase
for example if date of birth is 20/12/1960 and date of purchase is 16/01/2019 I need to have 27 which is 27 days after the birthday or if the date of purchase is 05/12/2018 I need -15 which is 15 days before the birthday.
Any suggestions?
Since you need to stay within the year of purchase, you need to extract the day of the year for the birthday and the purchase date which can be done using .dt.dayofyear as follows:
import pandas as pd
import numpy as np
df=pd.DataFrame({'customer_id':[1,2,3],
'birthday':pd.to_datetime(['20/12/1960','2/6/1980','6/1/1972']),
'purchase_date':pd.to_datetime(['1/1/2004','5/25/2018','3/4/2010'])})
df['days_away']=df['birthday'].dt.dayofyear - df['purchase_date'].dt.dayofyear
df
You can separate this into two. First, create a new column for the looked up birthday date. Second, subtract these two date columns to get the timedelata (and do .dt.days to get this in days).
Related
I have the following code which creates a range of dates from 1st Jan 2021. This runs until
15th May 2022.
import pandas as pd
range = pd.date_range('2021-01-01', periods=500, freq='D')
df = pd.DataFrame({ 'Date': range})
df.head()
I would like to use the current datetime data to create another column which provides tags
for weeks that are counted from every Thursday to Friday. This will be considered as 1 week count.
For example, the column should list date, month and year for each row of the matching 'range' column. This can be like:
10.03.21 to 18.03.21, for 10th March to 18th March (repeated for all rows for date ranges where it falls in range for Thursday to Friday each week).
Basically, my requirements for one week starts from Thursday, and ends the following week Friday.
For each week completion, another column should indicate the count, for e.g Week 1, Week 2 and etc.
How can this be achieved using Pandas datetime function, to easily produce another column with tagging for week categorisation ?
I try to keep only month and year in this df. I tried several solutions but it is not working. Can you help me ?
YOu need to do this (as you post no data, you'll need to adapt this to your case):
from datetime import datetime
datetime_object = datetime.now()
print(datetime_object)
2021-11-30 15:57:20.812209
And to get the year and month do this:
new_date_month= datetime_object.month
print(new_date_month)
new_date_year = datetime_object.year
print(new_date_year)
11
2021
If you need them as new columns in you df:
df['year']=datetime_object.year
df['Month']=datetime_object.month
Note that if your column is not a datetime, this will not work. Given to format of date you hve you will need to do this first:
st = '2021-11-30 15:57:20.812209'
datetime.strptime(st, '%Y-%m-%d %H:%M:%S.%f')
I have a forcast of 24 months in my Dataframe, how can I filter the date to 12 months
I know how to filter by a fixed date.
But my dates are always extended by one month. So I need a variable filter.
My solution should be to filter 12 months from the current month on.
Thanks a lot
Try this:
from datetime import date
from dateutil.relativedelta import relativedelta
df = df[df['Date_column_name'] >= (date.today() + relativedelta(months=+12))]
Hope it helps...
I have a table in a data model that has forecast figures for the next 3 months. What I want to do is to show what the forecast number for the current month to date is.
When I use the DATESMTD function like this:
=CALCULATE(SUM(InternetSales_USD[SalesAmount_USD]),DATESMTD(DateTime[DateKey]))
I get the last month of my data summarised as a total. I assume that is because the DATESMTD function takes the last date in the column and that is 3 months away.
How do I make sure I get this current month MTD total rather then the end of the calendar? The formula should be clever enough to realise I am in May and want the May MTD not the August MTD.
Any ideas?
The way to do this is to do this:
Forecast_Transaction_MTD:=CALCULATE(sum('ATO Online'[2017 Transaction Forecast]), DATESINPERIOD('ATO Online'[Current Year],TODAY(),-day(TODAY()),day))
the last -day(TODAY()) gets the day number for the current day and subtract it from today's date. So, today is the 25 May. the -day(TODAY())),day)) extracts the day (25) and subtracts it from the current date to get me to the 1 May.
The rest of the formula just adds the total for the dates.
I have an a table with two columns birthday and anniversary. I want to get alerts about birthdays and anniversaries between a 7 day period of time but, that should not include year (obviously if I include year, it would always be less than the current date). I want to get the alerts 7 days in advance.
That is, the query should compare the birthday and anniversary with the current date and return a list if their birthday or anniversary falls between 7 days of the same month so that it alerts me in advance about the upcoming birthdays and anniversaries.
Subtract the year difference from now to the requested date and then use datediff to calculate the date difference of the result with the requested date.
SELECT *
FROM Table
WHERE DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Birthday,GETDATE()),GETDATE()),Birthday) BETWEEN 0 AND 7
OR DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Anniversary,GETDATE()),GETDATE()),Anniversary) BETWEEN 0 AND 7
Try This
SELECT Name,max(Table .birthdate)
FROM Table group by Table .Name having (datediff(day,max(birthdate),getutcdate())>7 and datediff(day,max(birthdate),getutcdate())<8)