admin管理员组文章数量:1403371
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
1 Answer
Reset to default 2By 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')
本文标签: pythonNonExistentTime error caused by pandasTimestampfloor with localised timestampStack Overflow
版权声明:本文标题:python - NonExistentTime error caused by pandas.Timestamp.floor with localised timestamp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744413473a2605074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论