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.7k times
9

Harikrishna
- 1,100
- 5
- 13
- 30
-
I think [this](https://stackoverflow.com/a/8907269/2570277) answers your question. – Nick Nov 03 '18 at 09:36
4 Answers
7
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
- 822,522
- 95
- 1,334
- 1,252
-
1The only one of the 2 solutions provided that worked was the string manipulation. The timedelta difference still shows `0 days ` and then the time difference – LuizAngioletti Nov 04 '22 at 00:22
2
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,546
- 1
- 7
- 18

varun kumar nyalapelli
- 41
- 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
- 743
- 9
- 17
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.
- 237
- 4
- 12