Search code examples
pythonpandastimedelta

How to remove 0 day for and keep time only in a dataframe in pandas


Hi im having an issue with when converting the time into timedelta. I managed to keep the time only but it change to an object type. i would like to ask if you can assist me with keeping the time only in timedelta type. below is an example of my dataframe. | Time | ST |S| | ---------------| ---|-| | 0 days 12:09:46| 33 |R| | 0 days 12:09:51| 45 |H|

i tried the following

  n['Time'] = pd.to_timedelta(n['Time'])
# n['Time'] = pd.to_timedelta(n['Time'], unit='s') # error unit must not be specified if the input contains a str
  n['Time'] = n['Time'].astype(str).str.split('0 days ').str[-1] # works but cant 
  resample the data 
  n= n.set_index('Time')#.resample('6s').mean()


It was working before when i chaned to a new laptop 0 days appeared

Thanks for your time and help


Solution

  • We have to drop the column S contacting strings before resampling then 0 days won't affect resampling nor plotting as it is the same across all rows in the data

    n= n.drop(['s'], axis = 1)
    n['Time'] = pd.to_timedelta(n['Time']) 
    n = n.set_index('Time')#.resample('6s').mean()
    

    This works fine Thanks