如何让小时在 Pandas to_datetime 中可见?

How to make hour visible in Pandas to_datetime?

这是给定的数据框;

>>> df3
          15 Minutes         LINK NAME  secState
0   2021-01-11 00:00  hbretmask_sec10           0
1   2021-01-11 00:00  hgretmask_sec10           0
2   2021-01-11 00:00  hlretmask_sec10           0
3   2021-01-11 00:00  hmretmask_sec10           0
4   2021-01-11 00:00  hbretmask_sec11           0
5   2021-01-11 00:00  hgretmask_sec11           0
6   2021-01-11 00:00  hlretmask_sec11           0
7   2021-01-11 00:00  hmretmask_sec11           0
8   2021-01-11 00:00  hbretmask_sec12           0
9   2021-01-11 00:00  hgretmask_sec12           0
10  2021-01-11 00:00  hlretmask_sec12           0

我正在将“15 分钟”日期列重铸为日期时间格式;

df3["15 Minutes"] =  pd.to_datetime(df3["15 Minutes"],errors= "coerce",format='%Y-%m-%d %H:%M')

日期列中的小时字段正在消失;

>>> df3
   15 Minutes         LINK NAME  secState
0  2021-01-11  hbretmask_sec10           0
1  2021-01-11  hgretmask_sec10           0
2  2021-01-11  hlretmask_sec10           0
3  2021-01-11  hmretmask_sec10           0
4  2021-01-11  hbretmask_sec11           0
5  2021-01-11  hgretmask_sec11           0
6  2021-01-11  hlretmask_sec11           0
7  2021-01-11  hmretmask_sec11           0
8  2021-01-11  hbretmask_sec12           0
9  2021-01-11  hgretmask_sec12           0
10 2021-01-11  hlretmask_sec12           0

当我检查时,虽然它通常在那里..

>>> df3.iloc[0]
15 Minutes    2021-01-11 00:00:00
LINK NAME        hbretmask_sec10
secState                      0
Name: 0, dtype: object

这只会在 00:00 小时内发生。我怎样才能让小时可见?

要显示小时,您需要使用 'strftime':

df['Minutes'] = df['Minutes'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M'))