I have a column in a pandas dataframe that is created after subtracting two times. I now have a timedelta object like this -1 days +02:45:00. I just need to remove the -1 days and want it to be 02:45:00. Is there a way to do this?
Asked
Active
Viewed 1.1k times
5
Harikrishna
- 900
- 3
- 11
- 30
-
I think [this](https://stackoverflow.com/a/8907269/2570277) answers your question. – Nick Nov 03 '18 at 09:36
5 Answers
5
I think you can subtract days converted to timedeltas:
td = pd.to_timedelta(['-1 days +02:45:00','1 days +02:45:00','0 days +02:45:00'])
df = pd.DataFrame({'td': td})
df['td'] = df['td'] - pd.to_timedelta(df['td'].dt.days, unit='d')
print (df.head())
td
0 02:45:00
1 02:45:00
2 02:45:00
print (type(df.loc[0, 'td']))
<class 'pandas._libs.tslibs.timedeltas.Timedelta'>
Or convert timedeltas to strings and extract strings between days and .:
df['td'] = df['td'].astype(str).str.extract('days (.*?)\.')
print (df.head())
td
0 +02:45:00
1 02:45:00
2 02:45:00
print (type(df.loc[0, 'td']))
<class 'str'>
jezrael
- 729,927
- 78
- 1,141
- 1,090
1
I found this method easy, others didnt work for me
df['column'] = df['column'].astype(str).map(lambda x: x[7:])
It slices of the days part and you only get time part
swag2198
- 2,338
- 1
- 5
- 15
varun kumar nyalapelli
- 31
- 1
- 6
0
If your column is named time1, you can do it like this:
import pandas as pd
import datetime as dt
df['time1'] = pd.to_datetime(str(df.time1)[11:19]) #this slice can be adjusted
df['time1'] = df.time1.dt.time
this is going to convert the timedelta to str, slice the time part from it, convert it to datetime and extract the time from that.
Asad Rauf
- 683
- 11
- 15
0
I found a very easy solution for other people who may encounter this problem:
if timedelta_obj.days < 0:
timedelta_obj.days = datetime.timedelta(
seconds=timedelta_obj.total_seconds() + 3600*24)
Yaniv K.
- 177
- 1
- 12
0
I think what you want to do is to minus two timestamps while ignoring the date. One simple way to do that is to set the two timestamps with the same date before minus.
import pandas as pd
start_time = pd.Timestamp(2007, 12, 5, 21, 24, 36)
end_time = pd.Timestamp(2007, 12, 6, 20, 24, 36)
start_time - end_time # which returns Timedelta('-1 days +01:00:00')
What you can do is just set end_time with the same date as start_time:
import pandas as pd
start_time = pd.Timestamp(2007, 12, 5, 21, 24, 36)
end_time = pd.Timestamp(2007, 12, 6, 20, 24, 36)
end_time = end_time.replace(year=start_time.year, month=start_time.month, day=start_time.day)
start_time - end_time # which returns Timedelta('0 days +01:00:00')
Songhua Hu
- 141
- 1
- 3