pythonpandasdatetimetimestamp

NonExistentTime error caused by pandas.Timestamp.floor with localised timestamp


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

Solution

  • By default a non-existent time will raise an error.

    There is an nonexistent option in Timestamp.floor to shift the time forward/backward:

    (pd.Timestamp('2024-09-08 12:00:00-0300', tz='America/Santiago')
       .floor('D', nonexistent='shift_backward')
    )
    # Timestamp('2024-09-07 23:59:59-0400', tz='America/Santiago')
    
    (pd.Timestamp('2024-09-08 12:00:00-0300', tz='America/Santiago')
       .floor('D', nonexistent='shift_forward')
    )
    # Timestamp('2024-09-08 01:00:00-0300', tz='America/Santiago')
    

    Or, to get 23:00 passing pd.Timedelta('-1h') (not sure of the relevance to do this):

    (pd.Timestamp('2024-09-08 12:00:00-0300', tz='America/Santiago')
       .floor('D', nonexistent=pd.Timedelta('-1h'))
    )
    # Timestamp('2024-09-07 23:00:00-0400', tz='America/Santiago')