Converting Year - Week to days of the week - PYTHON - python

I have the following data:
Year&Week
Year
Week
2022-50
2022
50
2022-51
2022
51
2022-52
2022
52
I would like to convert that information by adding a new column that reflects the last day (sunday) and the initial day (monday) of the weeks. So my final dataframe should looks like the following:
Year&Week
Year
Week
Initial day of week
Last day of week
2022-50
2022
50
12/Dec/2022
18/Dec/2022
2022-51
2022
51
19/Dec/2022
25/Dec/2022
2022-52
2022
52
26/Dec/2022
01/Jan/2023

We can use pd.to_datetime and create date base:
%W: The week number of the year
%Y: The year number
%a: Weekday, short version
%b: The first three characters of the month name
%d: The day of the month
df['Initial day of week'] = pd.to_datetime(
df['Week'].astype(str) + df['Year'].astype(str) + 'Mon', format='%W%Y%a'
).dt.strftime("%d/%b/%Y")
df['Last day of week'] = pd.to_datetime(
df['Week'].astype(str) + df['Year'].astype(str) + 'Sun', format='%W%Y%a'
).dt.strftime("%d/%b/%Y")
print(df)
Output:
Year&Week Year Week Initial day of week Last day of week
0 2022-50 2022 50 12/Dec/2022 18/Dec/2022
1 2022-51 2022 51 19/Dec/2022 25/Dec/2022
2 2022-52 2022 52 26/Dec/2022 01/Jan/2023

You can use the python datetime module to convert 2022-50 which is probably is in the "%Y%W" Format to M/d/y format, like the below's peace of codes:
from datetime import datetime
input_date = "2022-50"
right_date = datetime.strptime(input_date, "%Y-%W")
formate_date_reult = right_date.strftime("%m/%d/%Y")
print(formate_date_reult) # Output is: 12/25/2022

Related

How to get the week number starting from monday of given month in python?

I was trying to calculate the week number starting from first Monday of October. Is there any functions in pandas or datetime to do the calculation efficiently?
MWE
import pandas as pd
from datetime import date,datetime
df = pd.DataFrame({'date': pd.date_range('2021-10-01','2022-11-01',freq='1D')})
df['day_name'] = df['date'].dt.day_name()
df2 = df.head()
df2['Fiscal_Week'] = [52,52,52,1,1] # 2021-10-4 is monday, so for oct4, week is 1
# How to do it programatically for any year?
df2
date day_name Fiscal_Week
0 2021-10-01 Friday 52
1 2021-10-02 Saturday 52
2 2021-10-03 Sunday 52
3 2021-10-04 Monday 1
4 2021-10-05 Tuesday 1
Shift dates by the number of days to new year, they use standard Monday week number formatting (%W):
df['Fiscal_Week'] = (
df['date'] + pd.DateOffset(days=91)
).dt.strftime('%W').astype(int).replace({0:None}).fillna(method='ffill')
The offset in days can be calculated manually (I assume the fiscal year start is fixed)
The replace part is needed because leftovers from the previous year are considered week 0. The previous week might be 52 or 53, so replacing with NA and then fill forward

Select last day of ISO-8601 week (and retain only year and week as object)

We have this df:
df = pd.DataFrame({
'date': [pd.Timestamp('2020-12-26'), # week 52 of year 2020
pd.Timestamp('2020-12-27'), # last day of week 52 of year 2020
pd.Timestamp('2021-03-10'), # week 10 of year 2021
pd.Timestamp('2022-01-03'), # first day of week 1 of year 2022
pd.Timestamp('2022-01-09')], # last day of week 1 of year 2022
'value' : [15, 15.5, 26, 36, 36.15]
})
We want a new df that looks so:
date value
0 202052 15.50
1 202201 36.15
In other words we need to:
convert 'date' to format year/week number (and store result as
an object)
select only rows which date correspond to the last
day of the week
Note both (1) and (2) need to be done following ISO-8601 definition of weeks. Actual dataset has thousands of rows.
How do we do this?
You can work directly on the series by using the dt call on the column to transform the format of the date. To find if it is the last day of the week, Sunday corresponds to 7 so we can do an equality check.
iso = df.date.dt.isocalendar()
mask = iso.day == 7
df.loc[mask].assign(date=iso.year.astype(str) + iso.week.astype(str).str.rjust(2, "0"))
date value
1 202052 15.50
4 202201 36.15

How do I extract last day of week from yyyyww column in python

I have a df_mixed column containing data in yyyyww format, eg: 201501, 201502…etc
I have to extract the last date of the week and put it in ds column.
For eg: For 201501, last day of week 1 is 4-1-2015
For 201502, last day is 11-1-2015
I have to follow the ISO format.
According to the ISO format the 1st week of 2015 starts from 29th December 2014 and ends on 4th January 2015
Any idea how to go about it using python, pandas and datetime library?
IIUC use pd.to_datetime to construct the datetime in format %Y%W%w. I added 0 as the weekday since you want Sundays which is first day of a week:
df = pd.DataFrame({"Date":[201501, 201502]})
df["Date"] = pd.to_datetime((df["Date"]-1).astype(str)+"0", format="%Y%W%w")
print (df)
Date
0 2015-01-04
1 2015-01-11
Assuming this input:
df = pd.DataFrame({'date': ['201501', '201502']})
If you choose Sunday as the last day of week:
df['date2'] = pd.to_datetime(df['date']+'Sun', format='%Y%W%a')
df
output:
date date2
0 201501 2015-01-11
1 201502 2015-01-18
NB. if you want American week format, use %U in place of %W and Mon as the last day of week. See the doc for datetime for more precisions

Get last friday of each month in python

I want last friday of each month for upcoming three months.
Friday_date = datetime.date.today()
while Friday_date.weekday() != 4:
Friday_date += datetime.timedelta(1)
This gives me the nearest friday. I want to make sure this is the last friday of this month so that i can add 28 days to get next friday.
The easiest way to do this is to use the module dateutil:
>>> from dateutil.relativedelta import FR, relativedelta
>>> datetime.date.today()+relativedelta(day=31, weekday=FR(-1))
datetime.date(2021, 6, 25)
Don't assume you can get the last Friday of subsequent months just by adding 28 days. It won't always work. Adding 28 days to the last Friday of February 2024 gives you this:
>>> datetime.date(2024,2,1)+relativedelta(day=31, weekday=FR(-1), days=28)
datetime.date(2024, 3, 22)
but the last Friday of that month is 29 March. Let dateutil do that correctly for you:
>>> datetime.date(2024,2,1)+relativedelta(day=31, weekday=FR(-1), months=1)
datetime.date(2024, 3, 29)
If needed with standard library only, here is with calendar and datetime:
import calendar
from datetime import date
today = date.today()
year, month = today.year, today.month
n_months = 4
friday = calendar.FRIDAY
for _ in range(n_months):
# get last friday
c = calendar.monthcalendar(year, month)
day_number = c[-1][friday] or c[-2][friday]
# display the found date
print(date(year, month, day_number))
# refine year and month
if month < 12:
month += 1
else:
month = 1
year += 1
where the line c[-1][friday] or c[-2][friday] first checks the last week of the month: is Friday nonzero there? if so take it, else look at the week before where there must be a Friday.
This prints
2021-06-25
2021-07-30
2021-08-27
2021-09-24
This formula gets you the day of the last Friday of any given month:
import calendar
year = 2021
month = 6
last_day = calendar.monthrange(year, month)[1]
last_weekday = calendar.weekday(year, month, last_day)
last_friday = last_day - ((7 - (4 - last_weekday)) % 7)
# ^ ^
# | Friday
# days in a week
This is my first coffee, so this can probably be condensed a bit, but it illustrates the logic. last_day is the last calendar day (30 for June), last_weekday is what weekday it is (2 for Wednesday), and based on that we simply calculate how many days to subtract to land on the last Friday (25).
If you want to know the last friday you can do this :
from datetime import date
from datetime import timedelta
today = date.today()
offset = (today.weekday() - 4) % 7
last_wednesday = today - timedelta(days=offset)

python - find year given day, month, and weekday name

I have a column of strings that give me month and day and day name as
Date Week Day
03-03 Friday
03-19 Saturday
03-18 Saturday
03-18 Monday
....
I want to return the most recent year, given those months and days, but also given the name of day.
so
Date Week Day Max Year
03-03 Friday 2018
03-19 Saturday 2016
03-18 Saturday 2017
03-18 Monday 2013
....
EDIT: Sorry forgot to add what I was thinking and trying
So I'm trying to do something like:
from datetime import datetime
day = 28
month = 3
name = 'Monday'
t = datetime.strptime('{}_{}_{}'.format(month,day,name), '%d_%m_%A')
t.strftime('%Y')
But it stores year as 1900. I'm just not sure how to get it to return the year as the most recent, or maxyear?
As far as I know, you will have to write a for loop to search for the most recent year. This shows an example of how you can do that.
import datetime
today = datetime.date.today() # This would be the user input date
weekday = 1 # look for the most recent Tuesday on today's date
while(today.weekday() != weekday and today.year > 1000):
today = today.replace(year=today.year - 1) # subtract one year from the date
if today.weekday() == weekday:
print("The Most recent Tuesday was in:",today.year)
break
if today.year <= 1000:
print("No Date found matching critieria since year 1000 AD")

Categories

Resources