Convert timedelta64[ns] column to seconds in Python Pandas DataFrame
This works properly in the current version of Pandas (version 0.14):
In [132]: df[:5]['duration'] / np.timedelta64(1, 's')
Out[132]:
0 1232
1 1390
2 1495
3 797
4 1132
Name: duration, dtype: float64
Here is a workaround for older versions of Pandas/NumPy:
In [131]: df[:5]['duration'].values.view('<i8')/10**9
Out[131]: array([1232, 1390, 1495, 797, 1132], dtype=int64)
timedelta64 and datetime64 data are stored internally as 8-byte ints (dtype'<i8'
). So the above views the timedelta64s as 8-byte ints and then does integer
division to convert nanoseconds to seconds.
Note that you need NumPy version 1.7 or newer to work with datetime64/timedelta64s.
Convert pandas timedelta to be displayed in seconds
Use the total_seconds()
method on the dt
accessor:
foo['DURATION_NEW'].dt.total_seconds()
Convert datetime64 to seconds
Off the top of my head (and with the poor information in the question), if there's a column that contains hours, minutes and seconds, which I will call "time" I would create the following column in the dataframe:
df['full_seconds'] = df['time'].dt.seconds
Or use a simple calculus summing hours, minutes and seconds:
df['full_seconds'] = df['time'].dt.hour * 3600 + df['time'].dt.minute * 60 + df['time'].dt.second
Convert timedelta64[ns] column to seconds in Python Pandas DataFrame
This works properly in the current version of Pandas (version 0.14):
In [132]: df[:5]['duration'] / np.timedelta64(1, 's')
Out[132]:
0 1232
1 1390
2 1495
3 797
4 1132
Name: duration, dtype: float64
Here is a workaround for older versions of Pandas/NumPy:
In [131]: df[:5]['duration'].values.view('<i8')/10**9
Out[131]: array([1232, 1390, 1495, 797, 1132], dtype=int64)
timedelta64 and datetime64 data are stored internally as 8-byte ints (dtype'<i8'
). So the above views the timedelta64s as 8-byte ints and then does integer
division to convert nanoseconds to seconds.
Note that you need NumPy version 1.7 or newer to work with datetime64/timedelta64s.
How to subtract seconds column from timedelta64 column in pandas?
Try using diff
instead:
example_table["x"] = example_table[["Start_Time", "Factor"]].diff(axis=1)['Factor']
Python: Pandas Dataframe How to Convert timedelta column to float column
You can use apply
and lambda
to access the attribute days
(https://pandas.pydata.org/pandas-docs/stable/timedeltas.html):
df_EVENT5_24['ColA'] = df_EVENT5_24.apply(lambda row: row.ColA.days, axis=1)
how to extract days as integers from a timedelta64[ns] object in python
This should convert your timedelta64[ns]
type to float64
representing days:
data['difference'].astype('timedelta64[D]')
Related Topics
How to Check If a Column Exists in Pandas
Why Can't I Use the Method _Cmp_ in Python 3 as for Python 2
Python Mixed Integer Linear Programming
Writing Utf-8 String to MySQL with Python
Compile Main Python Program Using Cython
Is the Shortcircuit Behaviour of Python's Any/All Explicit
Python Datetime to String Without Microsecond Component
Python Urllib2 with Keep Alive
Differencebetween 'Transform' and 'Fit_Transform' in Sklearn
Prevent Plot from Showing in Jupyter Notebook
Read from File After Write, Before Closing
Python (And Python C API): _New_ Versus _Init_
Pandas Concat Generates Nan Values