【问题标题】:How to add timedelta for each time seperately contain in one column using python如何使用python为每次单独包含在一列中添加timedelta
【发布时间】: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

My csv file

【问题讨论】:

  • 对你想要什么和你写什么很困惑。我猜 data['duration'] = data['duration'].apply(f) 存在缩进不匹配。然而,这行代码并没有改变任何东西。请提供您想要的最终输出示例,
  • @kantal 我希望你现在能理解我的问题。

标签: python python-3.x pandas time


【解决方案1】:

也许你就是这么想的:

My test data frame:
T= pd.DataFrame({"T":[ "00:00:00" for i in range(3) ]},index=np.random.randint(0,100,3))

           T
8   00:00:00
96  00:00:00
44  00:00:00

tims=[ dt.time(i).strftime("%H:%M:%S") for i in range(1,7)] 

['01:00:00', '02:00:00', '03:00:00', '04:00:00', '05:00:00', '06:00:00']


dd=T.apply(lambda r: pd.Series({"T":"00:00:00", "Hours":tims}), axis=1)

           T                                              Hours
8   00:00:00  [01:00:00, 02:00:00, 03:00:00, 04:00:00, 05:00...
96  00:00:00  [01:00:00, 02:00:00, 03:00:00, 04:00:00, 05:00...
44  00:00:00  [01:00:00, 02:00:00, 03:00:00, 04:00:00, 05:00...

dd.explode("Hours")

           T     Hours
8   00:00:00  01:00:00
8   00:00:00  02:00:00
8   00:00:00  03:00:00
8   00:00:00  04:00:00
8   00:00:00  05:00:00
8   00:00:00  06:00:00
44  00:00:00  01:00:00
44  00:00:00  02:00:00
44  00:00:00  03:00:00
44  00:00:00  04:00:00
44  00:00:00  05:00:00
44  00:00:00  06:00:00
96  00:00:00  01:00:00
96  00:00:00  02:00:00
96  00:00:00  03:00:00
96  00:00:00  04:00:00
96  00:00:00  05:00:00
96  00:00:00  06:00:00

【讨论】:

  • 是的,这就是我所期待的。我有一个问题,我可以在课堂上写这段代码吗?如果我想继续这段代码,那怎么写?
  • @team 你的课是什么?
  • 如果我写了 def convert_time(T): 在这个类里面我可以写你提供的这些代码吗?
  • @team "def convert_time(T):" 将是一个函数,而不是一个类。如果功能,是的。它必须返回 dd.explode("Hours")。
猜你喜欢
  • 2016-06-23
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 2019-11-17
相关资源
最近更新 更多