NonExistentTimeError when localizing pandas datetime index

柔情痞子 提交于 2021-01-27 09:53:47

问题


I am using dataframe.index = dataframe.index.tz_localize('Iran') to localize my Pandas dataframe datetime index. The problem is Django rises a NonExistentTimeError.

Django setting.py timezone is setted to my local time zone. TIME_ZONE = 'Iran'

Dataframe:

2014-08-11 12:00:00+00:00  3076.366
2014-08-11 11:45:00+00:00  3076.367
2014-08-11 11:30:00+00:00  3076.385
2014-08-11 11:15:00+00:00  3076.417
2014-08-11 11:00:00+00:00  3076.466
2014-08-11 10:45:00+00:00  3076.532
2014-08-11 10:30:00+00:00  3076.611
2014-08-11 10:15:00+00:00  3076.702
2014-08-11 10:00:00+00:00  3076.802
2014-08-11 09:45:00+00:00  3076.910

In [112]:dataframe.index.tzinfo
In [113]:<StaticTzInfo 'GMT'>

[10 rows x 1 columns]


回答1:


The exception NonExistentTimeError is raised from pytz when you attempt to localize a datetime that doesn't exist in the time zone you specified.

If you're wondering how time can "not exist", consider that local time in many time zones is affected by daylight saving time. It is also occasionally affected by changes made by local governments.

In your particular case, all of the values you showed were on August 11, 2014. Iran's daylight saving time fall-back transition doesn't occur until September 21, 2014, as shown here. If one of your values was 2014-09-21 11:30:00, then it would make sense. I can only conclude that you didn't show the actual data that caused the error. Please check your data.

Also, you should be using the full time zone name, which is 'Asia/Tehran'. While 'Iran' may work now, it is a backwards compatibility link only, and you should use the canonical zone name instead. See also this list on Wikipedia.



来源:https://stackoverflow.com/questions/25266675/nonexistenttimeerror-when-localizing-pandas-datetime-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!