from string to timedelta in pandas

后端 未结 4 1420
遥遥无期
遥遥无期 2020-12-19 21:58

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

相关标签:
4条回答
  • 2020-12-19 22:02

    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)
    
    0 讨论(0)
  • 2020-12-19 22:09

    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='m')
    
    print(s)
    
    0     1 days 10:23:00
    1     5 days 05:26:00
    2   634 days 18:52:00
    dtype: timedelta64[ns]
    
    0 讨论(0)
  • 2020-12-19 22:18

    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.

    0 讨论(0)
  • 2020-12-19 22:20

    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')
    
    0 讨论(0)
提交回复
热议问题