@@ -228,7 +228,7 @@ def fix_time_units_for_ems(
228228 """
229229
230230 with netCDF4 .Dataset (dataset_path , 'r+' ) as dataset :
231- variable = dataset .variables [variable_name ]
231+ variable = dataset .variables [str ( variable_name ) ]
232232
233233 units = cast (str , variable .getncattr ('units' ))
234234 calendar = cast (str , variable .getncattr ('calendar' ) or DEFAULT_CALENDAR )
@@ -668,7 +668,11 @@ def wind_dimension(
668668 return xarray .DataArray (data = new_data , dims = new_dims )
669669
670670
671- def datetime_from_np_time (np_time : numpy .datetime64 ) -> datetime .datetime :
671+ def datetime_from_np_time (
672+ np_time : numpy .datetime64 ,
673+ * ,
674+ tz : datetime .tzinfo = datetime .timezone .utc ,
675+ ) -> datetime .datetime :
672676 """
673677 Convert a numpy :class:`~numpy.datetime64`
674678 to a python :class:`~datetime.datetime`.
@@ -682,8 +686,24 @@ def datetime_from_np_time(np_time: numpy.datetime64) -> datetime.datetime:
682686 A conversion that truncates data is not reported as an error.
683687 If you're using numpy datetime64 with attosecond accuracy,
684688 the Python datetime formatting methods are insufficient for your needs anyway.
689+
690+ Parameters
691+ ==========
692+ np_time : numpy.datetime64
693+ The numpy datetime64 to convert to a Python datetime.
694+ tz : datetime.tzinfo
695+ The timezone that the numpy datetime is in.
696+ Defaults to UTC, as xarray will convert all time variables to UTC when
697+ opening files.
698+
699+ Returns
700+ =======
701+ datetime.datetime
702+ A timezone aware Python datetime.datetime instance.
685703 """
686- return datetime .datetime .fromtimestamp (np_time .item () / 10 ** 9 )
704+ epoc = numpy .datetime64 ('1970-01-01' )
705+ timestamp = (np_time - epoc ).astype ('timedelta64[ns]' )
706+ return datetime .datetime .fromtimestamp (timestamp .astype (float ) / 1e9 , tz = tz )
687707
688708
689709class RequiresExtraException (Exception ):
0 commit comments