如何删除 0 天并仅在 pandas 中的数据框中保留时间

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

您好,我在将时间转换为 timedelta 时遇到了问题。我设法只保留了时间,但它变成了一个对象类型。我想问一下你是否可以帮助我只用 timedelta 类型来保持时间。下面是我的数据框的一个例子。 |时间 | ST |S| | --------------| ---|-| | 0 天 12:09:46| 33|R| | 0 天 12:09:51| 45|H|

我尝试了以下方法

  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()


之前我换到一台新笔记本电脑时它一直在工作 0 days appeared

感谢您的宝贵时间和帮助

我们必须在重新采样之前删除 S 列联系字符串 0 天不会影响重采样或绘图,因为它在数据中的所有行中都是相同的

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

这很好用 谢谢