Change start of weekday from monday to sunday in datetimeindex in python - python-3.x

The default start of the weekday is Monday (0) in Pandas. Am trying to make this as Sunday instead on datetimeindex for getting weekly counts ending on Saturday to forecast a timeseries model.

I got a breakthrough, yes.
From my previous code, I redefined W-SAT instead of the default W.
Previous code:
weekreq = daysreq.resample('W').apply({'Numbers':'sum'})
to
New code:
weekreq = daysreq.resample('W-SAT').apply({'Numbers':'sum'})

Related

how to calculate the date from the year, week-of-year and day-of-week in EXCEL?

I have the Year, Week-of-Year and Day-of-the-Week as follows:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
and I would like to estimate the Date as dd.mm.yyyy, which is highlighted in yellow as it shows in the EXCEL picture.
I tried many formulas, but I am sure there might be an easy one.
I think you are counting the weeks starting from zero because for 9/1/2022 (YYYY/MM/DD format) the corresponding week is 36 as per the result of function WEEKNUM(DATE(2022,9,1)). In order to use the logic to multiply the number of weeks by 7. You need to use as a reference the first day of the year, if it was a Sunday, if not then go back to the previous Sunday, so you can count the entire week. Bottom line use as a reference date, the Sunday of the first week of the year, not the first day of the year (YYYY/1/1)
Here is the approach we use in cell E2:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1))
We use the LET function to avoid repeating the same calculation. The following expression finds the previous Sunday if the first day of the year (fDay) was not a Sunday:
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2))
The XLOOKUP function is used to get the numeric representation of the weekday and use the TEXT function to generate the weekdays in a long format. Since we count the entire week, if the weekday is a Sunday (column C in my screenshot), then we don't need to add any day to our reference date, that is why we use seq-1.
Here is the output for several sample data. Assuming the week count starts with zero, if not the formula needs to be adjusted as also the input data.
Notice that the year 2021 started on a Friday, so if we want to find a day for the first week (0) before Friday it will return a date from the previous year. Like in the case of Monday. If you want an error message instead, then the formula can be modified as follow:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
result, fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1),
IF(YEAR(result) <> y, "ERROR: Date from previous year", result))
I found the solution:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
=DATE (A2,1,3)-WEEKDAY(DATE(A2,1,3)) + 7 * B2 + C2 - 6
I found this solution, but you need to do further testing if it really works.
I calculate month from week: =+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)
I calculate week day number from week day name: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)
And then make date using: =DATE(A2;C2;E2)

moment.js how to set startOf week to monday on current week?

I want to make the date of startOf("week") its start from Monday on current week
Is it possible for make this? My code:
moment().startOf("week").toDate();
This startOf start from saturday proof startOf start from saturday
Take a look at the First Day of Week and First Week of Year section
From the docs (emphasis is mine)
Locale#week.dow should be an integer representing the first day of the week, 0 is Sunday, 1 is Monday, ..., 6 is Saturday.
So just by running this code before doing any calls to moment
// From 2.12.0 onward
moment.updateLocale('en', {
week : {
dow : 1
}
});
It will set the first day of the week to Monday.
After that calling moment().startOf("week").toDate() will give you a Monday.

how to set SimpleDateFormat W use Monday as the first day of the week instead of Sunday (US default)

how to set SimpleDateFormat "W" use Monday as the first day of the week instead of Sunday (US default)
I'm trying to extract the week of the month number with F.date_format("dates", "W")
but this would make an observation on the Sunday the first of week 2 instead of the last of week 1
The issue seems to be because simple date format isn't localised and defaults to us/default
Is there a way to set the locale to en/gb for simpleDateFormat to not treat use US defaults
https://issues.apache.org/jira/browse/SPARK-31879

Python pandas Calculate difference between 2 timestamp columns in a dataframe excluding weekends in seconds

I have 2 columns with timestamp on them, I need the difference between them in seconds in a 3rd column , excluding weekends. How am I supposed to do this in Python/pandas?
I want it to exclude Saturday/Sunday from the timeline.
Ex -
1 . Starts at Thursday/Friday and ends at Monday/Tuesday - Calculate duration only for the time it lied between Thursday/Friday and then directly Monday/Tuesday.
2 . If it starts on Saturday and ends on Monday - Calculate only for Monday.
3 . If ex.Starts on Friday and ends on Sunday, Calculate only for Friday.
4 . If starts and ends on Saturday and Sunday - result is 0 seconds
First, Convert the timestamp in both the columns to DateTime if not it's already in this format.
Second, find if the day is a weekend or a weekday and use total_seconds method to find the diff in seconds in the following way:
def find_diff_in_secs(row):
day_of_week = row["start"].weekday()
if day_of_week<5:
# Find the diff in secs
diff_in_secs =(row["end"]-row["start"]).total_seconds()
return diff_in_secs
else:
return "NA"
df.apply(find_diff_in_secs)

How to get / output all days in the current week in Automation Anywhere?

I'm attempting to output all days within the current week. e.g. for this week, show all days, 05/12/2019 through 05/18/2019 only. when the bot is executed next week, only show days 05/19/2019 through 05/25/2019. My current logic outputs the days for this week, but come tomorrow, the dates for this week will be thrown off. Please see the following
...could I get some help with this please?
Using VBS
I would do this using a VBS script, using Run Script command.
The default week start is Sunday you can change it check: https://www.w3schools.com/asp/func_weekday.asp
Pass the day you that you want as a parameter from 0 to 6, and get the data as a return value.
DayNumber: 0 = Sunday ..... 6 = Saturday
InputDate = Date
DayNumber = WScript.Arguments.Item(0)
Result = DateAdd("d", DayNumber - WeekDay(InputDate, 2), InputDate)
WScript.StdOut.Write(Result)
'MsgBox(Result)
Using MetaBot
Metabot Link: Change Date and Time Format
You will have to run the following logic in sequence.
Input: DayNumber: 0 = Sunday ..... 6 = Saturday
Using DayOfWeek Logic, Get the Day of the week and assign it to
WeekDay variable, it will return the name, not the number, and the input will be Date.
Using IF conditions convert the name of
the day to number, start from 0 to 6 as your first day in the week,
which is Sunday, and using variable operation assigns the value to
NumWeekDay variable.
Using variable operation, Get the offset by subtracting DayNumber, the day you want minus NumWeekDay,
and assign the value to Offset variable.
Using AddDays, Input
the date and the offset, and you will get the date of the day that you want.

Resources