Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NonExistentTimeError when localizing pandas datetime index

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]

like image 779
Moradi Avatar asked Aug 12 '14 14:08

Moradi


1 Answers

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.

like image 190
Matt Johnson-Pint Avatar answered Oct 03 '22 05:10

Matt Johnson-Pint