最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - NonExistentTime error caused by pandas.Timestamp.floor with localised timestamp - Stack Overflow

programmeradmin4浏览0评论

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

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
Share Improve this question asked Mar 20 at 11:27 edd313edd313 1,5092 gold badges10 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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')
发布评论

评论列表(0)

  1. 暂无评论