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

I have a dataframe as follows:

period
1651622400000.00000
1651536000000.00000
1651449600000.00000
1651363200000.00000
1651276800000.00000
1651190400000.00000
1651104000000.00000
1651017600000.00000

I have converted it into human readable datetime as:

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

and this outputs:

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

hours minutes and seconds are turned to 0.

I checked this into https://www.epochconverter.com/ and this gives

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

How do I get h, m, and s as well?

2 answers

  • answered 2022-05-04 10:44 jezrael

    If use https://www.epochconverter.com/ is added timezone.

    If need add timezones to column use 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
    

  • answered 2022-05-04 10:52 My Work

    There is no problem with your code or with pandas. And I don't think the timezone is an issue here either (as the other answer says). April 4, 2022 12:00:00 AM is the exact same time and date as 2022-04-04 00:00:00, just in one case you use AM... You could specify timezones as jezrael writes or with utc=True (check the docs) but I guess that's not your problem.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum