[英]Pandas Format time Nanoseconds
I am having a problem with formatting a time series in a pandas dataframe.我在格式化 pandas dataframe 中的时间序列时遇到问题。
00:18:41.728.560.640
is my time object.是我的时间 object。 It is hour:min:sec:ms:us:ns.
它是小时:分钟:秒:毫秒:我们:ns。
My approach was:我的方法是:
df['Time'] = pd.to_datetime(df['Time'], format="%H:%M:%S.%f")
Results in:结果是:
ValueError: unconverted data remains: .560.640
But this is only working for milliseconds.但这仅适用于几毫秒。 Is there another way to format the time or what am I doing wrong?
还有另一种格式化时间的方法还是我做错了什么?
Datetime struggles with the dots separating ms, us and ns.日期时间与分隔 ms、us 和 ns 的点作斗争。 You can remove them with regex:
您可以使用正则表达式删除它们:
df['Time'] = pd.to_datetime(df['Time'].replace(r'(?<!:\d\d)\.', '', regex=True), format="%H:%M:%S.%f")
We use a negative lookbehind (?<::\d\d)
to make sure the first dot isn't replaced by an empty string.我们使用否定的lookbehind
(?<::\d\d)
来确保第一个点不被空字符串替换。
Another option would be:另一种选择是:
pd.to_timedelta(df['Time'].str[::-1].str.replace('.', '', 2).str[::-1])
0
0 0 days 00:18:41.728560640
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.