SoFunction
Updated on 2024-11-17

Converting Timedelta to Int or Float in Python

Timedelta converts to Int or Float.

Pandas processing

import pandas as pd
dataSet['t'] =dataSet['time'].astype('timedelta64[D]').astype(float)
dataSet['t'] .head()

Numpy processing

import numpy as np
dataSet['t']=(dataSet['time']/np.timedelta64(1, 'D')).astype(float)
dataSet['t'] .head()

make a distinction

Method (a) pandas is to extract the days value directly, such as 90 days 04:48:00 to extract the value of 90;

Method (ii) numpy is converting the whole time, e.g. 90 days 04:48:00 into 90.200000.

You can choose different methods for time conversion according to your actual needs.

timedelta, float interconversion

在这里插入图片描述

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.