【问题标题】:How to convert time as 00:00:00 for specific value using python如何使用python将时间转换为特定值的00:00:00
【发布时间】:2020-01-09 00:29:11
【问题描述】:

我有一个包含一个输入和日期、时间的数据集。 我只想将输入列中包含的特定值的时间转换为 00:00:00,其他时间将按原样显示。

我尝试了一个代码,它给了我 00:00:00 的特定值,但其他时间显示为 NaT。

谁能帮我解决这个错误?

我的代码:

df['time_diff']= pd.to_datetime(df['date'] + " " + df['time'],
                    format='%d/%m/%Y %H:%M:%S', dayfirst=True)
mask = df['x3'].eq(5)
df['Duration'] = np.where(df['x3']== 5, df['time_diff'], np.datetime64('NaT') )
df['Duration'] = df['time_diff'].sub(df['Duration']).dt.total_seconds().div(3600)

然后它给了我这个输出:

date	  time	       x3	duration
10/3/2018	6:15:00	        0	NaN
10/3/2018	6:45:00  	5	00:00:00 
10/3/2018	7:45:00	        0	NaN
10/3/2018	9:00:00	        0	NaN
10/3/2018	9:25:00	        0       NaN
10/3/2018	9:30:00	        0	NaN
10/3/2018	11:00:00	0	NaN
10/3/2018	11:30:00	0	NaN
10/3/2018	13:30:00	0	NaN
10/3/2018	13:50:00	5	00:00:00
10/3/2018	15:00:00	0	NaN
10/3/2018	15:25:00	0	NaN
10/3/2018	16:25:00	0	NaN
10/3/2018	18:00:00	0	NaN
10/3/2018	19:00:00	0	NaN
10/3/2018	19:30:00	0	NaN
10/3/2018	20:00:00	0	NaN
10/3/2018	22:05:00	0	NaN
10/3/2018	22:15:00	5	00:00:00
10/3/2018	23:40:00	0	NaN
10/4/2018	6:58:00	        5	00:00:00
10/4/2018	13:00:00	0	NaN
10/4/2018	16:00:00	0	NaN
10/4/2018	17:00:00	0	NaN

但我期望的输出是:

date	  time	       x3	duration         expected output is
10/3/2018	6:15:00	        0	NaN                    6:15:00
10/3/2018	6:45:00  	5	00:00:00               00:00:00
10/3/2018	7:45:00	        0	NaN                    7:45:00
10/3/2018	9:00:00	        0	NaN                    9:00:00
10/3/2018	9:25:00	        0       NaN                    9:25:00
10/3/2018	9:30:00	        0	NaN                    9:30:00
10/3/2018	11:00:00	0	NaN                    11:00:00
10/3/2018	11:30:00	0	NaN                    11:30:00
10/3/2018	13:30:00	0	NaN                    13:30:00
10/3/2018	13:50:00	5	00:00:00               00:00:00
10/3/2018	15:00:00	0	NaN                    15:00:00
10/3/2018	15:25:00	0	NaN                    15:25:00
10/3/2018	16:25:00	0	NaN                    16:25:00
10/3/2018	18:00:00	0	NaN                    18:00:00
10/3/2018	19:00:00	0	NaN                    19:00:00
10/3/2018	19:30:00	0	NaN                    19:30:00
10/3/2018	20:00:00	0	NaN                    20:00:00
10/3/2018	22:05:00	0	NaN                    22:05:00
10/3/2018	22:15:00	5	00:00:00               00:00:00
10/3/2018	23:40:00	0	NaN                    23:40:00
10/4/2018	6:58:00	        5	00:00:00               00:00:00
10/4/2018	13:00:00	0	NaN                   13:00:00
10/4/2018	16:00:00	0	NaN                   16:00:00
10/4/2018	17:00:00	0	NaN                   17:00:00

【问题讨论】:

    标签: python-3.x pandas time


    【解决方案1】:

    使用 numpy.where 按条件创建新列 - 将 0 timedelta 和列 time 转换为 timedeltas:

    df['Duration'] = np.where(df['x3'].eq(5), np.timedelta64(0), pd.to_timedelta(df['time']))
    print (df)
             date      time  x3 Duration
    0   10/3/2018   6:15:00   0 06:15:00
    1   10/3/2018   6:45:00   5 00:00:00
    2   10/3/2018   7:45:00   0 07:45:00
    3   10/3/2018   9:00:00   0 09:00:00
    4   10/3/2018   9:25:00   0 09:25:00
    5   10/3/2018   9:30:00   0 09:30:00
    6   10/3/2018  11:00:00   0 11:00:00
    7   10/3/2018  11:30:00   0 11:30:00
    8   10/3/2018  13:30:00   0 13:30:00
    9   10/3/2018  13:50:00   5 00:00:00
    10  10/3/2018  15:00:00   0 15:00:00
    11  10/3/2018  15:25:00   0 15:25:00
    12  10/3/2018  16:25:00   0 16:25:00
    13  10/3/2018  18:00:00   0 18:00:00
    14  10/3/2018  19:00:00   0 19:00:00
    15  10/3/2018  19:30:00   0 19:30:00
    16  10/3/2018  20:00:00   0 20:00:00
    17  10/3/2018  22:05:00   0 22:05:00
    18  10/3/2018  22:15:00   5 00:00:00
    19  10/3/2018  23:40:00   0 23:40:00
    20  10/4/2018   6:58:00   5 00:00:00
    21  10/4/2018  13:00:00   0 13:00:00
    22  10/4/2018  16:00:00   0 16:00:00
    23  10/4/2018  17:00:00   0 17:00:00
    

    【讨论】:

    • 非常感谢您的快速回复。这就是我所期待的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 2014-02-19
    • 2016-08-10
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多