Python - Determine - python

I am trying to identify the month number from a given week and year number (both are user inputs). The script needs to be written in python.
eg - 1) Yr 2017, Wk 12 results in Month# 3
2) Yr 2017, Wk 32 results in Month#8 and so on
I am a newbie and searched for all date time examples, still no clue. Any ideas? Would really appreciate the help.
Thanks in advance!

As we know the numbers of days in each month of a given year, and the number of days that span w weeks (w*7) it is sufficient to sum the days of each month until the result is greater than w*7, counting the number of months used in the sum. If we used say n months, the number you want is n
We can know if the year is a leap year as the following demonstrates.
import calendar
print calendar.isleap(2017)

Related

How can I have the exact date for a certain week of the year? [duplicate]

This question already has answers here:
Pandas: How to create a datetime object from Week and Year?
(4 answers)
Closed 4 months ago.
I have the following data:
week = [202001, 202002, 202003, ..., 202052]
Where the composition of the variable is [year - 4 digits] + [week - 2 digits] (so, the first row means it's the first week of 2020, and so on).
I want to transform this, to a date-time variable [YYYY - MM - DD]. I'm not sure what day could fit in this format :( maybe the first saturday of every week.
week_date = [2020-01-04, 2020-01-11, 2020-01-18, ...]
It seems like a simple sequence, neverthless I have some missings values on the data, so my n < number of weeks of 2020.
The main purpose of this conversion is that I can have a fit model to train in prophet. I also think I need no missing values when incorporating the data into prophet, so maybe the answer could be also adding 0 to my time series?
Any ideas? Thanks
Try:
l = [202001, 202002, 202003, 202052]
out [datetime.datetime.fromisocalendar(int(x[:4]), int(x[4:]), 6).strftime("%Y-%m-%d") for x in map(str,l)]
print(out)
outputs:
['2020-01-04', '2020-01-11', '2020-01-18', '2020-12-26']
Here I used 6 as the week day but chose as you want
This makes a datetime object from the first and last part of each number after mapping them to a string, then outputs a string back with strftime and the right format.

Python: Find date of monday of a given calender week in a given year

I have a questions about Python.
My goal is the following: Let a year and a calender week be given. Then I want to find the calender date of the Monday of this calender week.
I want to use the calender week standard ISO 8601, see https://en.wikipedia.org/wiki/Week#Numbering
I have written the following code:
def calWeekMonday(year,calWeek):
firstDayOfYear = int(dt.date(year,1,1).strftime("%w")) # first day of year as a number
if calWeek > 53:
return "There exists no calender week greater than 53."
elif calWeek == 53 and firstDayOfYear != 4:
# a year has 53 calender weeks if and only if it starts with Thursday
return "There is no calender week 53 in this year."
else:
if firstDayOfYear < 4: # then Jan 1st is in week 1 of the new year
mondayWeek1 = dt.date(year, 1, 1) + dt.timedelta(days=-(firstDayOfYear - 1))
else: # then Jan 1st is in last week of old year
mondayWeek1 = dt.date(year, 1, 1) + dt.timedelta(days=7 - firstDayOfYear + 1)
# calculate monday of calWeek
wantedMonday = mondayWeek1 + dt.timedelta(weeks=(calWeek - 1))
return wantedMonday
I have tested some examples and they work, but I am worried that I haven't considered all special cases (there are a loooot of them). Maybe someone can help me with the following questions:
Is there any way to make sure that my code works for ALL special cases?
Does anyone see a mistake in my code?
Does anyone have any improvements?
Is there a faster/easier way to do it, for example, is there any package which might help me (I tried to find something, but I wasn't successfull).
I am happy about any help and want to say thank you in advance!
At the moment, your function works for most inputs. Of course, if you are handling week 53+, then do you want to handle non-positive inputs? If you give a zero/negative week-number then the function gives a date from a different year, is this what you want?
Negative years give an error (obviously), do you want to handle that?
Finally, you haven't considered leap years in your '53-week years must start on Thursday' rule (2020 was 53-week but started on Wednesday)

AutoArima - Selecting correct value for m

So for argument sake here is an example of autoarima for daily data:
auto_arima(df['orders'],seasonal=True,m=7)
Now in that example after running a Seasonal Decomposition that has shown weekly seasonality I "think" you select 7 for m? Is this correct as the seasonality is shown to be weekly?
My first question is as follows - If seasonality is Monthly do you use 12? If it is Annually do you use 1? And is there ever a reason to select 365 for daily?
Secondly if the data you are given is already weekly e.g
date weekly tot
2021/01/01 - 10,000
2021/01/07 - 15,000
2021/01/14 - 9,000
and so on......
And you do the seasonal decomposition would m=1 be used for weekly, m=4 for monthly and m=52 for annually.
Finally if its monthly like so:
date monthly tot
2020/01/01- 10,000
2020/02/01- 15,000
2020/03/01- 9,000
and so on......
And you do the seasonal decomposition would m=1 for monthly and m=12 for annually.
Any help would be greatly appreciated, I just want to be able to confidently select the right criteria.
A season is a recurring pattern in your data and m is the length of that season. m in that case is not a code or anything but simply the length:
Imagine the weather, if you had the weekly average temperature it will rise in the summer and fall in the winter. Since the length of one "season" is a year or 52 weeks, you set m to 52.
If you had a repeating pattern every quarter, then m would be 12, since a quarter is equal to 12 weeks. It always depends on your data and your use case.
To your questions:
If seasonality is Monthly do you use 12?
If the pattern you are looking for repeats every 12 months yes, if it repeats every 3 months it would be 3 and so on.
If it is Annually do you use 1?
A seasonality of 1 does not really make sense, since it would mean that you have a repeating pattern in every single data point.
And is there ever a reason to select 365 for daily?
If your data is daily and the pattern repeats every 365 days (meaning every year) then yes (you need to remember that every fourth year has 366 days though).
I hope you get the concept behind seasonality and m so you can answer the rest.

Deducting x number of months from a given date

Is there a way to deduct a specified number of months from a given date. So for example, if the day is 2006/02/27. Then I want to backtrack 3 months and find the months within this date and 3 months back. In this case, it would be Feb, Jan & dec. What I am really after is finding a range of months.
I can think of using timedelta and specifying 93 days (31 x 3). But this could potentially be a problem if its early month date. something like 01/03/2006 - 93 days will perhaps result in a date in november/2005, which will include march, Feb,Jan,Dec,Nov as months. But what I want is March,Feb and Jan
from datetime import datetime,timedelta
someDate = datetime(2006,2,27)
newDate =someDate - timedelta(days = 3)
#someDate - 3months
Any ideas on how to solve?

How to choose earliest NRF week number, from a set of date ranges in python?

I have a list of 104 consecutive weeks. The week numbers come from the National Retail Foundation Calendar. So week 1 will basically be the first week in February.
I'm finding ranges of weeks inside this two year list of dates. I'll assume just two ranges for the point of my question. I want to pick the weeks that give me the largest range.
When the ranges are in the middle of the years, it's easy, I just pick the earliest start, and latest end.
Example:
Range 1: 2010-04-09 (NRF Week 10) through 2010-07-16 (NRF Week 24)
Range 2: 2011-04-01 (NRF Week 9) through 2011-06-24 (NRF Week 21)
I'll choose start of 9, end of 24. Just a simple min() or max() function.
When the ranges start/end around the start/end of the year, this gets more complicated.
Example:
Range 1: 2010-02-12 (NRF Week 2) through 2010-05-14 (NRF Week 15).
Range 2: 2011-01-28 (NRF Week 52) through 2011-04-29 (NRF Week 13).
I need to choose start 52, end 15. min() no longer works for the start.
Or even worse, if the weeks are 1 and 47, I need to choose 47.
I'd like to find a way that will pick the best (earliest or latest) in both situations.
I'm not finding a straight forward approach to doing this. Has anyone run into this problem before or may be able to offer a solution?
I appreciate any help you might be able to give.
Thank you.
It looks to me like you simply want to ignore the year in your comparison function (to find the min/max dates). Since your data set is small, why not just iterate over all weeks and perform comparisons based on month & date only?

Categories