Unidata/cftime

always create cftime datetime instances by default in num2date

jswhit opened this issue · 4 comments

Currently, a python datetime instance is returned if possible. This can be controlled by the use_only_cftime_datetimes kwarg, which is currently False by default. PR #135 switches the default to True to avoid surprising results such as discussed in issue #134.

Will this cause problems with existing code? Is anyone relying on the current default?

At least from the perspective of xarray this should not be a problem; we already always use use_only_cftime_datetimes=True.

cf-python uses use_only_cftime_datetimes=True, as well, so no problem for us.

PR #135 merged. use_only_cftime_datetimes=True is now the default.

I have to complain that the way this change was implemented has been a bit annoying. To maintain compatibility between pre and post cftime 1.1 versions as we upgrade various systems I have resorted to do:

            try:
                time_dt = nc.num2date(time, nc_in.variables["time"].units, 
                        only_use_cftime_datetimes=False,
                        only_use_python_datetimes=True)
            except TypeError:
                # backwards compat with cftime less than 1.1, fall back with no kwargs
                # TypeError: num2date() got an unexpected keyword argument only_use_python_datetimes
                time_dt = nc.num2date(time, nc_in.variables["time"].units)

Since cftime was strict with kwargs in earlier versions, the new kwargs only_use_cftime_datetimes and only_use_python_datetimes are not accepted.

At least several libraries we use break with DatetimeGregorian, including so far spacepy and matplotlib, and we're only starting to upgrade. Am I misunderstanding something here?