python dataframe 将纪元转换为可读的日期时间小时分钟秒为零

python dataframe convert epoch to readable datetime hour minutes seconds as zero

我有一个数据框如下:

period
1651622400000.00000
1651536000000.00000
1651449600000.00000
1651363200000.00000
1651276800000.00000
1651190400000.00000
1651104000000.00000
1651017600000.00000

我已将其转换为人类可读的日期时间:

df['period'] = pd.to_datetime(df['period'], unit='ms')

这输出:

2022-04-04 00:00:00
2022-04-05 00:00:00
2022-04-06 00:00:00
2022-04-07 00:00:00
2022-04-08 00:00:00
2022-04-09 00:00:00
2022-04-10 00:00:00
2022-04-11 00:00:00
2022-04-12 00:00:00

时分秒归零

我将其检查到 https://www.epochconverter.com/,这给出了

GMT: Monday, April 4, 2022 12:00:00 AM
Your time zone: Monday, April 4, 2022 5:45:00 AM GMT+05:45

如何获得 h、m 和 s?

如果使用 https://www.epochconverter.com/ 添加时区。

如果需要在列中添加时区,请使用 Series.dt.tz_localize and then Series.dt.tz_convert:

df['period'] = (pd.to_datetime(df['period'], unit='ms')
                  .dt.tz_localize('GMT')
                  .dt.tz_convert('Asia/Kathmandu'))
print (df)
                     period
0 2022-05-04 05:45:00+05:45
1 2022-05-03 05:45:00+05:45
2 2022-05-02 05:45:00+05:45
3 2022-05-01 05:45:00+05:45
4 2022-04-30 05:45:00+05:45
5 2022-04-29 05:45:00+05:45
6 2022-04-28 05:45:00+05:45
7 2022-04-27 05:45:00+05:45

您的代码或 pandas 都没有问题。而且我认为时区在这里也不是问题(正如另一个答案所说)。 2022 年 4 月 4 日 12:00:00 AM 与 2022-04-04 00:00:00 的时间和日期完全相同,只是在一种情况下您使用 AM...您可以将时区指定为 jezrael 写入或者使用 utc=True(检查 docs),但我想这不是你的问题。