I have a pandas dataframe that has two datetime64 columns and one timedelta64 column that is the difference between the two columns. I'm trying to plot a histogram of the timedelta column to visualize the time differences between the two events.
However, just using df['time_delta']
results in: TypeError: ufunc add cannot use operands with types dtype('<m8[ns]') and dtype('float64')
Trying to convert the timedelta column to : float--> df2 = df1['time_delta'].astype(float)
results in: TypeError: cannot astype a timedelta from [timedelta64[ns]] to [float64]
How would one create a histogram of pandas timedelta data?
Timedelta. Represents a duration, the difference between two dates or times. Timedelta is the pandas equivalent of python's datetime. timedelta and is interchangeable with it in most cases.
Here are ways to convert timedeltas, docs are here
In [2]: pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s') Out[2]: 0 0 days, 00:00:01 1 1 days, 00:00:01 2 2 days, 00:00:01 3 3 days, 00:00:01 4 4 days, 00:00:01 dtype: timedelta64[ns]
Convert to seconds (is an exact conversion)
In [3]: (pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s')).astype('timedelta64[s]') Out[3]: 0 1 1 86401 2 172801 3 259201 4 345601 dtype: float64
Convert using astype will round to that unit
In [4]: (pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s')).astype('timedelta64[D]') Out[4]: 0 0 1 1 2 2 3 3 4 4 dtype: float64
Division will give an exact repr
In [5]: (pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s')) / np.timedelta64(1,'D') Out[5]: 0 0.000012 1 1.000012 2 2.000012 3 3.000012 4 4.000012 dtype: float64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With