timedelta to string type in pandas dataframe
It is possible by:
df['duration1'] = df['duration'].astype(str).str[-18:-10]
But solution is not general, if input is 3 days 05:01:11
it remove 3 days
too.
So solution working only for timedeltas less as one day correctly.
More general solution is create custom format:
N = 10
np.random.seed(11230)
rng = pd.date_range('2017-04-03 15:30:00', periods=N, freq='13.5H')
df = pd.DataFrame({'duration': np.abs(np.random.choice(rng, size=N) -
np.random.choice(rng, size=N)) })
df['duration1'] = df['duration'].astype(str).str[-18:-10]
def f(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('{}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds))
df['duration2'] = df['duration'].apply(f)
print (df)
duration duration1 duration2
0 2 days 06:00:00 06:00:00 54:00:00
1 2 days 19:30:00 19:30:00 67:30:00
2 1 days 03:00:00 03:00:00 27:00:00
3 0 days 00:00:00 00:00:00 0:00:00
4 4 days 12:00:00 12:00:00 108:00:00
5 1 days 03:00:00 03:00:00 27:00:00
6 0 days 13:30:00 13:30:00 13:30:00
7 1 days 16:30:00 16:30:00 40:30:00
8 0 days 00:00:00 00:00:00 0:00:00
9 1 days 16:30:00 16:30:00 40:30:00
How to convert timedelta column in pandas to string
As long as your columns are datetime
s, they have a .dt
accessor with lots of useful datetime methods attached, including strftime
, so you can do:
import pandas as pd
df = pd.DataFrame({'dates': pd.date_range('2015-01-01', '2015-01-10', freq='12H')})
df.dates.dt.strftime('%H:%S')
Apply change to timedelta to columns containing a given string
You can apply
pandas.to_timedelta
on all columns selected by filter
and update
the original dataframe:
df.update(df.filter(like='_SEL').apply(pd.to_timedelta, unit='s'))
NB. there is no output, the modification is inplace
updated dataframe:
date time_SEL time_02_SEL time_03_SEL other
0 2022-01-01 0 days 09:39:16 2 days 16:47:12 39 days 17:27:03 756
1 2022-01-03 0 days 06:28:42 0 days 01:12:23 0 days 00:55:34 343
2 2022-02-01 1 days 10:13:52 0 days 00:54:02 0 days 06:30:23 434
3 2022-03-01 84 days 18:13:52 0 days 09:00:23 3 days 17:50:23 34324
update "TypeError: invalid type promotion"
ensure you have numbers:
(df.update(df.filter(like='_SEL')
.apply(lambda c: pd.to_timedelta(pd.to_numeric(c, errors='coerce'),
unit='s'))
)
Turn a string back into a datetime timedelta
Use pd.to_timedelta
pd.to_timedelta(df.iloc[:, 0])
0 0 days 00:00:57.416000
1 0 days 00:00:12.036000
2 0 days 16:46:23.127000
3 49 days 00:09:30.813000
4 50 days 00:39:31.306000
5 55 days 12:39:32.269000
6 -1 days +22:03:05.256000
Name: 0, dtype: timedelta64[ns]
Dataframe - mean of string type column with time values
You can use regex to extract hours and minutes. To calcualte the mean time in minutus:
h = df['time'].str.extract('(\d{1,2})h').fillna(0).astype(int)
m = df['time'].str.extract('(\d{1,2})m').fillna(0).astype(int)
(h * 60 + m).mean()
Result:
0 83.75
dtype: float64
Related Topics
How to Get Md5 Sum of a String Using Python
Cannot Import Qtwebkitwidgets in Pyqt5
How to Annotate Types of Multiple Return Values
About the Pil Error -- Ioerror: Decoder Zip Not Available
Finding Occurrences of a Word in a String in Python 3
How to Resize an Image with Opencv2.0 and Python2.6
Kivy Not Working (Error: Unable to Find Any Valuable Window Provider.)
Installing Numpy and Scipy on 64-Bit Windows (With Pip)
JSON.Loads Allows Duplicate Keys in a Dictionary, Overwriting the First Value
Is There a Difference Between Continue and Pass in a for Loop in Python
Why Is 'Object' an Instance of 'Type' and 'Type' an Instance of 'Object'
Remove All Line Breaks from a Long String of Text
Selenium Python Error: Element Could Not Be Scrolled into View
Python: Slicing a Multi-Dimensional Array
How to Get a List of All Indices of Repeated Elements in a Numpy Array