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
Related
This question already has answers here:
Pandas - Number of Months Between Two Dates
(6 answers)
Best way to find the months between two dates
(41 answers)
Closed 1 year ago.
I have a dataframe with multiple dates, such as '2019-05-01' and I want to substract it to get e.g. 4 (2019-09-01 - 2019-05-01). Those are two columns.
df['delta'] = [ (x.year - y.year) * 12 + (x.month - y.month) for x, y in zip(df['date1'],df['date2'])]
This question already has answers here:
Pandas: extract hour from timedelta
(4 answers)
Pandas: Subtracting two date columns and the result being an integer
(5 answers)
Subtracting Dates to get Days in pandas
(1 answer)
Closed 2 years ago.
I have a dataset like this
data = pd.DataFrame({'order_date-time':['2017-09-13 08:59:02', '2017-06-28 11:52:20', '2018-05-18 10:25:53', '2017-08-01 18:38:42', '2017-08-10 21:48:40','2017-07-27 15:11:51',
'2018-03-18 21:00:44','2017-08-05 16:59:05', '2017-08-05 16:59:05','2017-06-05 12:22:19'],
'delivery_date_time':['2017-09-20 23:43:48', '2017-07-13 20:39:29','2018-06-04 18:34:26','2017-08-09 21:26:33','2017-08-24 20:04:21','2017-08-31 20:19:52',
'2018-03-28 21:57:44','2017-08-14 18:13:03','2017-08-14 18:13:03','2017-06-26 13:52:03']})
I need to calculate the delivery delay for this data
I did this to change it to a dattime data
data['order_date-time']=pd.to_datetime(data['order_date-time'])
data['delivery_date_time']=pd.to_datetime(data['delivery_date_time'])
then I calculated the
data['delivery delay']= data['delivery_date_time']-data['order_date-time']
and new column looks like this in the output
7 days 14:44:46
15 days 08:47:09
...
how can I change these column values to int values like 7, 15, .. without "days" and time?
Subtracting two datetime columns from each other gives you a column of dtype timedelta. You can call the days attribute of a timedelta column with the dt accessor:
data['delivery delay'].dt.days
0 7
1 15
2 17
3 8
...
...or if you need fractional days, call the total_seconds and divide by the seconds in a day:
data['delivery delay'].dt.total_seconds()/86400
0 7.614421
1 15.366076
2 17.339271
3 8.116563
...
Unfortunately, you can't format timedelta to string as you can with datetime, see also Format timedelta to string.
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
This question already has answers here:
How to create a DateTime equal to 15 minutes ago?
(9 answers)
Closed 2 years ago.
I'm using python to get time of 5 minutes ago.
Code:
import datetime
import time
now = datetime.datetime.now()
current_time = now.strftime(f"%Y-%m-%d %H:%M")
print(current_time)
2020-07-27 08:35:00
My question is how to get the time of 5 minutes ago.
something like
current_time-5mins
You may use datetime.timedelta():
datetime.datetime.now() - datetime.timedelta(minutes=5)
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