Hello any ideas how can convert timedelta object to float in pandas it needs to generalize based on minutes or hours i.e 00:06:46.221000 to 6.46 minutes or 01:06:76.341000 to 66.76 minutes
thanks
Hello any ideas how can convert timedelta object to float in pandas it needs to generalize based on minutes or hours i.e 00:06:46.221000 to 6.46 minutes or 01:06:76.341000 to 66.76 minutes
thanks
If you are using Python2.7/3.2 or higher, you can use the following method (where time_delta_obj is your timedelta object):
time_minutes = time_delta_obj.total_seconds()/60.0
In Python 3.2 or higher, you could simply use this:
time_minutes = time_delta_obj/datetime.timedelta(minutes=1)
See Python 3.2 - datetime and time and timedelta.total_seconds() for more information.
To apply this to a pandas dataframe, use apply():
def convert_to_minutes(time_delta_obj):
return time_delta_obj.total_seconds()/60.0
or
def convert_to_minutes(time_delta_obj):
return time_delta_obj/datetime.timedelta(minutes=1)
Then for the column named 'time_diff' of the df use this:
df['time_diff'] = df['time_diff'].apply(convert_to_minutes)
You can just divide your Series
with a Timedelta(minutes=1)
. Indeed:
>>> df
col
0 00:06:46.221000
1 00:03:12.456000
2 00:02:07.123456
>>> df['col'] / pd.Timedelta(minutes=1)
0 6.770350
1 3.207600
2 2.118724
Name: col, dtype: float64