How to calculate date of birth, given age expressed in years, months, days, as reported on a given date? - python-3.x

I have a 2 months old dataset, from 15 October 2020, containing the ages of different persons. The age of each person is given in years, months, days.
Example
Input: Reference date = 15 October 2020
One entry in the input data set could be:
Input: Age = 21 years, 0 months, 10 days
Corresponding output: Date of Birth = 25 September 1999
This takes into account that their age was reported on 15 October 2020.
I have to convert this data set to get the Date of Birth of each person. How can I accomplish that?

You can use the dateutil.relativedelta function, which you can use to subtract years, months and days from another date.
I'll assume that you know the exact date on which the data was collected, ... let's say 15 October 2020:
from datetime import date
from dateutil.relativedelta import relativedelta
# Let's say the data was taken on 15 October 2020:
refdate = date(2020, 10, 15)
# The data itself: triplets of years, months, days as collected on 15 October 2020:
data = [
[20, 2, 1],
[53, 5, 10],
]
# Extend the data with the birthdate:
for person in data:
person.append(refdate - relativedelta(years=person[0],
months=person[1],
days=person[2]))
# Print the data:
for person in data:
print("{} years, {} months, {} days: born on {}".format(*person))

Related

how to find birth year from age input?

I want to write a Python program that would take a user's age in years and will return their birth year. I tried to use the datetime module and find the current year, then subtract the age from the current year. Unfortunately, for certain birth months it's not giving me the right year. For example, if an user who was born in April 9 in 1990 inputs his birthday as 32 (since it's still January 2023, not April), he will get 1991 as his birth year which would be wrong, because he does not turn 33 until April 9, 2023. Also there are leap years. I don't know how to determine the birthday only from taking age as input. Any suggestions? Or is there any alternate ways of doing it?
This is my code:
from datetime import date
age = input("What's your age?")
today = date.today()
current_year=today.year
birth_year=current_year-age
But if I try giving 32 as age input for someone who has not yet turned 33 in the January 2023 (will turn 33 in April), then it will give me 1991 as the birth month, which is wrong.

Using pandas, how can I find out if my customer made a purchase last month or two months ago?

I'm new to python and new to pandas. Of course, if my project used exact dates, I could easily do this, but unfortunately, the date type is a little different, and as you can see, the sign 08 is after the year 1401, which means it is the eighth month of the year 1401.
I currently know that these 3 customers have bought from me this month. But I want to know if these 3 customers bought from me in the previous month or two months ago? If they do, I will give them a discount.
Of course, I should also say that the number 08 is not always fixed, but it could be 09 in the next month. I just want to know if they bought from me 1 month ago or not?
According to the picture, now only Sara should get a discount
You could convert the purchase date to an integer and calculate the number of months from there.
For instance, you have the purchase month 1901/07 and you want to know in 1901/08 how many months the last purchase took place. So you convert both values to integers and subtract them (190108 - 190107 = 1).
import pandas as pd
df = pd.DataFrame({'customer': ['david', 'sara'], 'date': ['1901/03', '1901/07']})
# Manually setting the reference month (190108 for Year 1901 and Month 08)
df['months'] = 190108 - df['date'].replace('/', '', regex=True).astype(int)
# Check if eligible for discount
df['discount'] = df['months'].isin([1, 2])
customer
date
months
discount
0
david
1901/03
5
False
1
sara
1901/07
1
True
To compare with today's month you could to the following:
df['months'] = int(pd.Timestamp.now().strftime('%Y%m'))\
- df['date'].replace('/', '', regex=True).astype(int)

US convention different in spreadsheet functions (Libre, Google Sheets, etc.)

The Excel/Google-Sheets/LibreOffice function DAYS360() returns the number of days between two dates based on a 360-day year. 0 (default) is used for the US-based method and here are some examples
A = 30 Apr 2016, B = 29 Feb 2016, DAYS360(A, B) = -61
A = 29 Feb 2016, B = 30 Apr 2016, DAYS360(A, B) = 60
This seems ok according to the rules here
But the Excel/Google-Sheets/LibreOffice function YEARFRAC() returns the number of years, including fractional years, between two dates using a specified day count convention. Even here 0 (default) uses US method, (US (NASD) 30/360) which I presumed will also be equal to the value of number of days calculated by DAYS360 * the number of seconds in a day/number of seconds in 360 days. The values in the sheets are as follows
A = 30 Apr 2016, B = 29 Feb 2016, YEARFRAC(A, B) = 0.1666666667
A = 29 Feb 2016, B = 30 Apr 2016, YEARFRAC(A, B) = 0.1666666667
Since it can be seen that the absolute value of the DAYS360 is different by one, the YEARFRAC value is same and assumes 60 days according to the presumption made above, so are the US-based convention mentioned here is the same as mentioned for DAYS360.
If not, what are the exact rules for this one, or is there some other problem?
NOTE: Tested these values on Google Sheets and Libre Office.
DAYS360 parameter 3:
0 indicates the US method - Under the US method, if start_date is the last day of a month, the day of month of start_date is changed to
30 for the purposes of the calculation. Furthermore if end_date is the
last day of a month and the day of the month of start_date is earlier
than the 30th, end_date is changed to the first day of the month
following end_date, otherwise the day of month of end_date is changed
to 30.
1 or any other value indicates the European method - Under the European method, any start_date or end_date that falls on the 31st of
a month has its day of month changed to 30.
YEARFRAC parameter 3:
0 indicates US (NASD) 30/360 - This assumes 30 day months and 360 day
years as per the National Association of Securities Dealers standard,
and performs specific adjustments to entered dates which fall at the
end of months.
1 indicates Actual/Actual - This calculates based upon the actual
number of days between the specified dates, and the actual number of
days in the intervening years. Used for US Treasury Bonds and Bills,
but also the most relevant for non-financial use.
2 indicates Actual/360 - This calculates based on the actual number of
days between the specified dates, but assumes a 360 day year.
3 indicates Actual/365 - This calculates based on the actual number of
days between the specified dates, but assumes a 365 day year.
4 indicates European 30/360 - Similar to 0, this calculates based on a
30 day month and 360 day year, but adjusts end-of-month dates
according to European financial conventions.

How do I know if today is a day due to change civil local time e.g. daylight saving time in standard python and pandas timestamps?

According to the rules of British Summer Time / daylight saving time (https://www.gov.uk/when-do-the-clocks-change) the clocks:
go forward 1 hour at 1am on the last Sunday in March,
go back 1 hour at 2am on the last Sunday in October.
In 2019 this civil local time change happens on March 31st and October 27th, but the days slightly change every year. Is there a clean way to know these dates for each input year?
I need to check these "changing time" dates in an automatic way, is there a way to avoid a for loop to check the details of each date to see if it is a "changing time" date?
At the moment I am exploring these dates for 2019 just to try to figure out a reproducible/automatic procedure and I found this:
# using datetime from the standard library
march_utc_30 = datetime.datetime(2019, 3, 30, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
march_utc_31 = datetime.datetime(2019, 3, 31, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
april_utc_1 = datetime.datetime(2019, 4, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
# using pandas timestamps
pd_march_utc_30 = pd.Timestamp(march_utc_30) #, tz='UTC')
pd_march_utc_31 = pd.Timestamp(march_utc_31) #, tz='UTC')
pd_april_utc_1 = pd.Timestamp(april_utc_1) #, tz='UTC')
# using pandas wrappers
pd_local_march_utc_30 = pd_march_utc_30.tz_convert('Europe/London')
pd_local_march_utc_31 = pd_march_utc_31.tz_convert('Europe/London')
pd_local_april_utc_1 = pd_april_utc_1.tz_convert('Europe/London')
# then printing all these dates
print("march_utc_30 {} pd_march_utc_30 {} pd_local_march_utc_30 {}".format(march_utc_30, pd_march_utc_30, pd_local_march_utc_30))
print("march_utc_31 {} pd_march_utc_31 {} pd_local_march_utc_31 {}".format(march_utc_31, pd_march_utc_31, pd_local_march_utc_31))
print("april_utc_1 {} pd_april_utc_1 {} pd_local_april_utc_1 {}".format(april_utc_1, pd_april_utc_1, pd_local_april_utc_1))
The output of those print statements is:
march_utc_30 2019-03-30 00:00:00+00:00 pd_march_utc_30 2019-03-30 00:00:00+00:00 pd_local_march_utc_30 2019-03-30 00:00:00+00:00
march_utc_31 2019-03-31 00:00:00+00:00 pd_march_utc_31 2019-03-31 00:00:00+00:00 pd_local_march_utc_31 2019-03-31 00:00:00+00:00
april_utc_1 2019-04-01 00:00:00+00:00 pd_april_utc_1 2019-04-01 00:00:00+00:00 pd_local_april_utc_1 2019-04-01 01:00:00+01:00
I could use a for loop to find out if the current date is the last Sunday of the month, or compare the "hour delta" between the current date and the date of the day after to see if there is a +1, but I am wondering if there is a cleaner way to do this?
Is there something attached to the year e.g. knowing the input year is 2019 then we know for sure the "change date" in March will be day 31st?
Using dateutil.rrule can help (install with pip install python-dateutil).
Because we can fetch dates by weeks, we don't need any loops,
from dateutil.rrule import rrule, WEEKLY
from dateutil.rrule import SU as Sunday
from datetime import date
import datetime
def get_last_sunday(year, month):
date = datetime.datetime(year=year, month=month, day=1)
# we can find max 5 sundays in a months
days = rrule(freq=WEEKLY, dtstart=date, byweekday=Sunday, count=5)
# Check if last date is same month,
# If not this couple year/month only have 4 Sundays
if days[-1].month == month:
return days[-1]
else:
return days[-2]
def get_march_switch(year):
# Get 5 next Sundays from first March
day = get_last_sunday(year, 3)
return day.replace(hour=1, minute=0, second=0, microsecond=0)
def get_october_switch(year):
day = get_last_sunday(year, 10)
return day.replace(hour=2, minute=0, second=0, microsecond=0)
print('2019:')
print(' {}'.format(get_march_switch(2019)))
print(' {}'.format(get_october_switch(2019)))
print('2021:')
print(' {}'.format(get_march_switch(2021)))
print(' {}'.format(get_october_switch(2021)))
get_sundays() returns the 5 next sundays from the first day of the given month, because a month can have maximum 5 sundays.
Then I just check (within get_(march|october)_switch()) if the last given sunday is from the expected month, if not well this month only have 4 sunday, I took this one.
Finally I fix the hours, seconds and microseconds.
Output:
2019:
2019-03-31 01:00:00
2019-10-27 02:00:00
2021:
2021-03-28 01:00:00
2021-10-24 02:00:00
I know the topic is quite old now. However, I had the same question today, and at the end I found a solution which seems quite simple to me, using only the standard datetime:
I want to check whether my date refdate is the October DST day - I did it in the following way:
refdate is my standard datetime object.
If you have a panda timestamp, you can convert it to native datetime using .to_pydatetime()
if refdate.month == 10 and refdate.weekday() == 6 and (refdate + dt.timedelta(weeks = 1)).month == 11:
oct_dst = 1

total number of days from current date if we want to know like after 2 weeks, 3 months , 1 year

I want to know the total number of days from current date.
if given date is 2 Feb 2018. and i want to calculate number of days after 2 weeks/3 months/1 year. keeping in mind of leap year or not.
Language - python3
Is there any library in python which give me this.
The dateutil module has a way to do this very conveniently. Sample code:
import datetime
import dateutil
today = datetime.date.today()
delta = dateutil.relativedelta.relativedelta(years=1, months=3, weeks=2)
desired_date = today + delta
desired_date
Output:
datetime.date(2019, 5, 16)

Resources