Subtract datetime to have delta in months [duplicate] - python

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'])]

Related

CONVERT Date column to %m-%d-%y [duplicate]

This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
How to change the datetime format in Pandas
(8 answers)
Closed last month.
I want to convert the date column in my dataframe which is in the format of Dec. 01, 2022 to 12-01-2022
I tried using map function and by importing calender as well

How to add - to the cell that has dates [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
How can I format date time string which is not date time format so that I can use it with pd.to_datetime()?
(2 answers)
Convert DataFrame column type from string to datetime
(6 answers)
Closed 5 months ago.
I have a 'date' column that has a date value as 20170423 (yyyymmdd) how can i change it to 2017-04-23?
dataframe = df
column name 'date'
I read the below post, but none of the solutions worked
Fastest way to insert these dashes in python string?
Example
d = df['Date']
df['Date'] = '%s-%s-%s' % (d[:4], d[4:6], d[6:])
The output 0 0 20170711\n1 20170718\n2 20170718\n3... when i exported in .csv

How to parse an 8 digit number as a date? [duplicate]

This question already has answers here:
How to convert integer into date object python?
(4 answers)
How to convert a specific integer to datetime in python
(1 answer)
int to datetime in Python [duplicate]
(1 answer)
Closed 1 year ago.
I need to build a way to automate file names, and I was curious as to if there is a function or a quick way to take in 8 digits and output the corresponding Date.
Specifically I only need the Month and year.
Example: 03232021 -> Mar2021
I was trying pandas to_datetime but it didn't seem like it was what I needed.
In [133]: s = "03232021"
In [134]: dt.datetime.strptime(s, "%m%d%Y").date()
Out[134]: datetime.date(2021, 3, 23)

seperate on special part of a datetime data and change it to int [duplicate]

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.

Remove "days 00:00:00"from dataframe [duplicate]

This question already has answers here:
Pandas Timedelta in Days
(5 answers)
Closed 3 years ago.
So, I have a pandas dataframe with a lot of variables including start/end date of loans.
I subtract these two in order to get their difference in days.
The result I get is of the type i.e. 349 days 00:00:00.
How can I keep only for example the number 349 from this column?
Check this format,
df['date'] = pd.to_timedelta(df['date'], errors='coerce').days
also, check .normalize() function in pandas.

Categories