Convert column of Dataframe to time [duplicate] - python

This question already has answers here:
Convert number to time in Python
(3 answers)
Closed 2 months ago.
I have the following Dataframe:
Now i want to convert the column "ABZEIT" to time format. So the first rows would be:
13:05 15:40 14:20 16:30 07:40 12:05 17:15

A solution that first defines a to_time() function then apply it to the dataframe by using map().
from datetime import time
def to_time(t: int) -> time:
t = f"{t:04}" # add the leading zero if needed
return time(int(t[:2]), int(t[2:]))
df["ABZEIT"] = df["ABZEIT"].map(to_time)

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

pandas calculate time difference to now [duplicate]

This question already has answers here:
Compare timestamp with datetime
(2 answers)
Timestamp String in Zulu Format To Datetime
(1 answer)
Closed 9 months ago.
I have a pandas dataframe df with a time column containing datetime values. I now want to filter the dataframe to show rows with time values lying in the next 15 minutes.
So first I try to simply subtract the current time from the datetimes.
df.Time = pd.to_datetime(df.Time)
print(df.Time - pd.to_datetime("today"))
But got this error:
TypeError: Cannot subtract tz-naive and tz-aware datetime-like objects
I tried to remove the tz-awareness with .replace(tzinfo=None) but it was not working. In the end I am looking for a command like this (assuming the difference of two datetimes in is minutes):
df.loc[df.Time - pd.to_datetime("today") < 15]

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.

How to find the timestamp difference between two files [duplicate]

This question already has answers here:
How do I find the time difference between two datetime objects in python?
(21 answers)
Closed 3 years ago.
When I execute ll command I get the timestamps:
-rw-rw-r--+ 1 4167 May 5 17:19 file A
-rw-rw-r--+ 1 2721 May 4 17:08 file B
I want the difference between timestamps of A and B
I tried this:
datetime.fromtimestamp(getmtime(file)).strftime('%h %m %s'))
It gives
May 05 1557032395
May 04 1557084082
Please help me get the time difference
Looks like you need.
import os
import datetime
print(datetime.datetime.fromtimestamp(os.path.getmtime("file A")) - datetime.datetime.fromtimestamp(os.path.getmtime("file A")))
You can subtract 2 datetime objects to get the difference.

Categories