【发布时间】:2019-09-09 08:57:01
【问题描述】:
这里我有一个数据集,其中包含输入以及日期和时间。在这里,我只想将输入列中包含的特定值的时间转换为 00:00:00,其他时间将按原样显示。然后我为此编写了代码。然后我想要的是仅指定 00:00:00 。所以我为它写了代码。
这是我的代码:
data['time_diff']= pd.to_datetime(data['date'] + " " + data['time'],
format='%d/%m/%Y %H:%M:%S', dayfirst=True)
data['duration'] = np.where(data['X3'].eq(5), np.timedelta64(0), pd.to_timedelta(data['time']))
print (data['duration'].dtype)
def f(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('{:02d}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds))
data['duration'] = data['duration'].apply(f)
match_time="00:00:00"
T = data.loc[data['duration'] == match_time, 'duration']
然后我得到了输出:
然后我想要做的是我只想为每个时间序列添加 6 小时然后我为它编写了代码,它只给了我 0 个值而没有单独的。
我的代码:
def time (y):
S=[]
row=0
for row in range(len(T)):
y = "00:00:00"
while row >0:
S = np.array(y + np.timedelta(hours=i) for i in range(6))
row += 1
break
else:
continue
#break
return
A= T.apply(time)
print(A)
然后输出来了:
但我的预期是:
T add timedelta 1hr till to 6 hrs expected output
00:00:00 01:00:00
" 02:00:00
03:00:00
04:00:00
" 05:00:00
06:00:00
00:00:00 " 01:00:00
02:00:00
03:00:00
04:00:00
05:00:00
06:00:00
00:00:00:00 01:00:00
02:00:00
03:00:00
04:00:00
05:00:00
06:00:00
【问题讨论】:
-
对你想要什么和你写什么很困惑。我猜 data['duration'] = data['duration'].apply(f) 存在缩进不匹配。然而,这行代码并没有改变任何东西。请提供您想要的最终输出示例,
-
@kantal 我希望你现在能理解我的问题。
标签: python python-3.x pandas time