How to calculate number of weeks between two ISO dates [duplicate] - python

This question already has answers here:
What's the best way to find the inverse of datetime.isocalendar()?
(8 answers)
Closed 2 years ago.
I am reading excel files line by line where I have in one column ISO year_week_start : 2020_53 and in second ISO year_week_end : 2021_01
I am looking how to get distance between these two strings
2021_01 - 2020_53 => 1
2020_40 - 2020_30 => 10

Is this what you are looking for?
from datetime import date
def convert(value):
(year, week) = [int(x) for x in value.split('_')]
result = date.fromisocalendar(year, week, 1)
return result
def distance(date1, date2):
date1 = convert(date1)
date2 = convert(date2)
diff = date1 - date2
weeks = diff.days / 7
return weeks
print(distance('2021_01', '2020_53'))
print(distance('2020_40', '2020_30'))
print(distance('2021_01', '1991_01'))
Output
1.0
10.0
1566.0

Related

python get the previous week number as YYYYWW [duplicate]

This question already has answers here:
How can I get the previous week in Python?
(1 answer)
How to get Year-Week format in ISO calendar format?
(2 answers)
Closed last month.
I have a year and week number in an integer format, and I would like to get the previous week number.
I currently have this code:
# get previous week number (as an integer)
weeknumber = 202302
year = weeknumber//100
week = weeknumber-year*100
day = 1
prev_weeknumber = int(''.join(map(str,((datetime.fromisocalendar(year,week,day))- timedelta(days = 7)).isocalendar()[0:2])))
But it provides the following output
prev_weeknumber = 20231
While I need it to have this output
prev_weeknumber = 202301

How can I get number of days since 1/1/1972 [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 2 months ago.
How can I get the number of days since 1/1/1972 with CustomerSince (a data frame) below: 
CustomerSince
1 2011-07-24 21:27:16.617
2 2011-10-05 21:27:16.617
3 2012-07-24 21:27:16.617
4 2010-08-31 21:27:16.617
5 2011-07-24 21:27:16.617
Thanks for the reply.
If your question is the difference between two dates, This solution may help you:
from datetime import datetime
str_dt1 = '1972/01/01'
str_dt2 = '2011/07/24'
dt1 = datetime.strptime(str_dt1, "%Y/%m/%d")
dt2 = datetime.strptime(str_dt2, "%Y/%m/%d")
delta = dt2 - dt1
print(f'Difference is {delta.days} days') # Difference is 14449 days

Python - convert number of hours to a date? [duplicate]

This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 2 years ago.
I have in Python 3.7 a date in form of an integer that represents the number of hours from 1/1/1900 00 Hours. Can I transform it into string of format dd/mm/yyyy?
for example:
timenumber = 1043148
timestring = magictrickfunctions(timenumber)
print(timestring)
should give "01/01/2017"
Are you sure it gives you 01/01/2017 instead of 01/01/2019?
To obtain the number of days divide your total hours by /24 this will give you the number of days. You can then specify that as shown below. I am using pandas library here.
import pandas as pd
start_date = "01/01/1900"
date_1 = pd.to_datetime(start_date)
end_date = date_1 + pd.DateOffset(days=43464) #specify the number of days and add it to your start_date
print(end_date)
Outtput
2019-01-01 00:00:00

Round Date in Pandas Dataframe [duplicate]

This question already has an answer here:
Pandas: convert date in month to the 1st day of next month
(1 answer)
Closed 5 years ago.
I'm attempting to round the date from this dataframe below to the 1st day of the next month - i.e. 1997-10-10 would result in 1997-11-01:
Date
1997-10-10
1997-05-27
1997-04-30
1997-12-19
1997-08-12
Currently my code looks like:
df = pd.read_csv(XXX)
df['Date'] = pd.to_datetime(df.Date)
df['Date'] = df['Date'].dt.date.replace(day = 1)
According to the python library documentation,
date.replace(year=self.year, month=self.month, day=self.day)
Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified. For example, if d == date(2002, 12, 31), then d.replace(day=26) == date(2002, 12, 26).
I had presumed that I could use .replace with only 1 argument - day -however I'm receiving a number of errors.
I think you need MonthBegin(0):
df['Date'] = pd.to_datetime(df.Date) + pd.offsets.MonthBegin(0)
print (df)
Date
0 1997-11-01
1 1997-06-01
2 1997-05-01
3 1998-01-01
4 1997-09-01

how to get week number on python? [duplicate]

This question already has answers here:
Python: Number of the Week in a Month
(9 answers)
Closed 7 years ago.
I want to get week number like picture below
If I insert 20150502, It should print "week 1".
If I insert 20150504, It should print "week 2".
If I insert 20150522, It should print "week 4".
How to get week number?
Here's a quick attempt, as with all date/time code there are probably a lot of edge cases that can cause strange results.
# dateutil's parser is very good for converting from string to date
from dateutil.parser import parse
date_strs = ['20150502', '20150504', '20150522']
for date_str in date_strs:
d = parse(date_str)
month_start = datetime.datetime(d.year, d.month, 1)
week = d.isocalendar()[1] - month_start.isocalendar()[1] + 1
print(date_str + ':', week)
Output:
20150502: 1
20150504: 2
20150522: 4

Categories