我想将 timedelta64[ns] 转换为 int
timedelta64[ns] 示例:01:00:00、02:00:00、03:00:00、04:00:00
我想转换为: 1, 2 , 3 , 4
请帮帮我T^T
我想将 timedelta64[ns] 转换为 int
timedelta64[ns] 示例:01:00:00、02:00:00、03:00:00、04:00:00
我想转换为: 1, 2 , 3 , 4
请帮帮我T^T
你可以做:
t, _ , _ = "01:00:00".split(":")
接着:
int(t)
那将返回您:1。
把它作为一个函数放在一起将是:
def time_to_integer(time):
t, _,_ = time.split(":")
return int(t)
请注意,您应该将时间变量作为字符串传递。例如:
time_to_integer("02:00:00")
将返回:2
In [106]: t = pd.to_timedelta(['01:12:34', '02:00:00', '03:00:00', '04:00:00'])
In [107]: t.seconds // 3600
Out[107]: Int64Index([1, 2, 3, 4], dtype='int64')