[Python] NonExistentTime error caused by pandas.Timestamp.floor with localised timestamp

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I need to calculate the floor of a localized timestamp with daily resolution, but I get an exception when the daylight saving time starts.

    >>> pd.Timestamp('2024-09-08 12:00:00-0300', tz='America/Santiago').floor("D")
    NonExistentTimeError: 2024-09-08 00:00:00


    I understand that midnight does not exist on that day, clocks are moved to 1am after 11:59pm. Still I would have expected floor to return pd.Timestamp('2024-09-08 01:00:00-0300', tz='America/Santiago').

    The same happens with ceil applied to the previous day:

    >>> pd.Timestamp('2024-09-07 12:00:00-0300', tz='America/Santiago').ceil("D")
    NonExistentTimeError: 2024-09-08 00:00:00


    I made two attempts at solving this but I either get the same exception or the wrong answer:

    >>> pd.Timestamp('2024-09-08 12:00:00').floor("D").tz_localize('America/Santiago')
    NonExistentTimeError: 2024-09-08 00:00:00
    >>> pd.Timestamp('2024-09-08 12:00:00-0300').floor("D").tz_convert('America/Santiago')
    Timestamp('2024-09-07 23:00:00-0400', tz='America/Santiago') # Wrong answer

    Continue reading...

Compartilhe esta Página