简体   繁体   English

从 timedelta 索引中删除天数?

[英]Removing days from timedelta index?

I have a dataframe df1 with index:我有一个带有索引的 dataframe df1

0 days 11:45:39
0 days 11:45:39
0 days 12:45:39

However, I am trying to remove the days portion of this.但是,我正在尝试删除其中的天数部分。 Is there a way to do this?有没有办法做到这一点? The dtype of index currently is timedelta index 当前的 dtype 是 timedelta

There's always the option of formatting it as a string, if all you want to do is use it for plotting labels:如果您只想使用它来绘制标签,则始终可以选择将其格式化为字符串:

# if the index is a string,
# df.index = pd.to_timedelta(df.index, errors='coerce')
(pd.to_datetime('2020') + df.index).strftime('%H:%M:%S')
# Index(['11:45:39', '11:45:39', '12:45:39'], dtype='object', name='delta')

Another option using the power of list comprehensions:使用列表推导的另一个选项:

[str(dt) for dt in  df.index.to_pytimedelta()]
# ['11:45:39', '11:45:39', '12:45:39']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM