Since I want to get the last Wendnesday of every month, use pendulum.parse but sometimes 4 is last week or 5 is last week. How to get easily the day is last Wendnesday of month or not?
enter image description here
You can use the calendar module from the standard library and compute all dates
calendar.weekday(year, month, day) Returns the day of the week (0 is
Monday) for year (1970–…), month (1–12), day (1–31).
As a helper, you can also use from the same module
calendar.monthrange(year, month) Returns weekday of first day of the
month and number of days in month, for the specified year and month.
Related
Given a day of the week (Monday, for instance), how do I find the next day that will be that weekday (as a datetime object)?
I just asked the question so people could see my solution as a function if they needed it. It uses the Python datetime module, and returns a datetime for the next day it will be that weekday.
def find_next_weekday(weekday_to_find): #with a string day of the week (full day of week, ie 'Monday'), return the next datetime object that will match that day
now = datetime.datetime.now() #Right now as a datetime object
for i in range(1,8):#the next instance of a weekday will have to be within this range
weekday = now + datetime.timedelta(days=i)
if (weekday.strftime("%A") == weekday_to_find):
return weekday
I'm running Python 3.8.3 and I found something weird about the ISO Week format (%V) :
The first day and the last day of 2019 are both in week 1.
from datetime import date
print(date(2019, 1, 1).strftime('%Y-W%V'))
print(date(2019, 12, 29).strftime('%Y-W%V'))
print(date(2019, 12, 31).strftime('%Y-W%V'))
Output:
2019-W01
2019-W52
2019-W01
Why does it behave like that?
It is fully correct.
As you see in your dates, all of them are in 2019, so it is correct to get 2019 with %Y.
Week number is defined by ISO, and so one week could be considered in previous or in next year.
You need to use %G to get year of the week number (%V).
I'm trying to generate week number string using Python time module, considering week starts on Sunday.
If my interpretation of the official documentation is correct then this can be achieved by the following code:
import time
time.strftime("%U", time.localtime())
>> 37
My question is, is the above output correct? Shouldn't the output be 38 instead, considering the below details:
My timezone is IST (GMT+5:30)
import time
#Year
time.localtime()[0]
>> 2019
#Month
time.localtime()[1]
>> 9
#Day
time.localtime()[2]
>> 18
Yes, the output is correct. Week 1 started on January 6th, as that was the first Sunday in 2019. January 1st through 5th were week 0:
>>> time.strftime('%U', time.strptime("2019-1-1", "%Y-%m-%d"))
'00'
>>> time.strftime('%U', time.strptime("2019-1-6", "%Y-%m-%d"))
'01'
This is covered in the documentation:
All days in a new year preceding the first Sunday are considered to be in week 0.
You are perhaps looking for the ISO week date, but note that in this system the first day of the week is a Monday.
You can get the week number using that system with the datetime.date.isocalendar() method, or by formatting with %V:
>>> time.strftime("%V", time.localtime())
'38'
>>> from datetime import date
>>> date.today().isocalendar() # returns ISO year, week, and weekday
(2019, 38, 2)
>>> date.today().strftime("%V")
'38'
It's correct since you start counting from the first Sunday.
%U - week number of the current year, starting with the first Sunday as the first day of the first week
https://www.tutorialspoint.com/python/time_strftime.htm
It's correct. Since all days in a new year preceding the first Sunday are considered to be in week 0 (01/01 to 01/05), this week is the week 37.
If we use the following way, we can find the Monday as week start date
df['week_start'] = df['myday'].dt.to_period('W').apply(lambda r: r.start_time)
The to_period('W') will default to use Monday as week start date. Is there anyway we can revise this function to change it to calculate Sunday as week start date? So every date will convert to the corresponding sunday in order to group by the data into weekly level.
For example 1/1/2017 will still be 1/1/2017, but 1/2/2017 to 1/7/2017 will be convert to 1/1/2017.
I revise the calculation in the following way to return Sunday instead of Monday
df['week_start'] = (df['myday']+timedelta(days=1)).dt.to_period('W').apply(lambda r: r.start_time)-timedelta(days=1)
Date is datetime.date(2013, 12, 30)
I am trying to get week number using
import datetime
datetime.date(2013, 12, 30).isocalendar()[1]
I am getting output as ,
1
Why i am not getting week number of last year , instead i am getting week number of current year?
Whats wrong i am doing here ?
You are doing nothing wrong, 2013/12/30 falls in week 1 of 2014, according to the ISO8601 week numbering standard:
The ISO 8601 definition for week 01 is the week with the year's first Thursday in it.
The Thursday in that week is 2014/01/02.
Other ways to explain the definition, from the same linked WikiPedia article:
It is the first week with a majority (four or more) of its days in January (ISO weeks start on Monday)
Its first day is the Monday nearest to 1 January.
It has 4 January in it. Hence the earliest possible dates are 29 December through 4 January, the latest 4 through 10 January.
It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days.
If you were looking for the last week number of a given year (52 or 53, depending on the year), I'd use December 28th, which is always guaranteed to be in the last week (because January 4th is always part of the first week of the next year):
def lastweeknumber(year):
return datetime.date(year, 12, 28).isocalendar()[1]
from datetime import date
from datetime import datetime
ndate='10/1/2016'
ndate = datetime.strptime(ndate, '%m/%d/%Y').strftime('%Y,%m,%d')
print('new format:',ndate)
d=ndate.split(',')
wkno = date(int(d[0]),int(d[1]),int(d[2])).isocalendar()[1]
print(wkno)
manually or read a date to a string and get the week number, play around with different formats.