问题
I have a dataframe where the timestamp is in the format HHHHH:MM
timestamp = pd.Series(['34:23','125:26','15234:52'], index=index)
I would like to convert it to a timedelta.
For now I manage to do that on a single string
str[:-3]
str[-2:]
timedelta(hours=int(str[:-3]),minutes=int(str[-2:]))
I would like to apply it to the whole serie, if possible in a cleaner way. Does it exist?
Thanks,
回答1:
You can use column-wise Pandas methods:
s = pd.Series(['34:23','125:26','15234:52'])
v = s.str.split(':', expand=True).astype(int)
s = pd.to_timedelta(v[0], unit='h') + pd.to_timedelta(v[1], unit='s')
print(s)
0 1 days 10:00:23
1 5 days 05:00:26
2 634 days 18:00:52
dtype: timedelta64[ns]
回答2:
This is how I would do it:
timestamp = pd.Series(['34:23','125:26','15234:52'])
x = timestamp.str.split(":").apply(lambda x: int(x[0])*60 + int(x[1]))
timestamp = pd.to_timedelta(x, unit='s')
回答3:
Parse the delta in seconds as an argument to pd.to_timedelta
like this,
In [1]: import pandas as pd
In [2]: ts = pd.Series(['34:23','125:26','15234:52'])
In [3]: secs = 60 * ts.apply(lambda x: 60*int(x[:-3]) + int(x[-2:]))
In [4]: pd.to_timedelta(secs, 's')
Out[4]:
0 1 days 10:23:00
1 5 days 05:26:00
2 634 days 18:52:00
dtype: timedelta64[ns]
Edit: missed erncyp's answer which would work as well but you need to multiply the argument to pd.to_timedelta
by 60 since if I recall correctly minutes aren't an available as a measure of elapsed time except modulo the previous hour.
回答4:
You can use pandas.Series.apply, i.e.:
def convert(args):
return timedelta(hours=int(args[:-3]),minutes=int(args[-2:]))
s = pd.Series(['34:23','125:26','15234:52'])
s = s.apply(convert)
来源:https://stackoverflow.com/questions/53543061/from-string-to-timedelta-in-pandas