Python Forum

Full Version: Cannot convert from timedelta64[ns] to timedelta64[h]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have this piece of code that used to work. I have upgraded Numpy and Pandas and now they don't seem to work.

 def astype(self, dtype, copy: bool = True):
        # We handle
        #   --> timedelta64[ns]
        #   --> timedelta64
        # DatetimeLikeArrayMixin super call handles other cases
        dtype = pandas_dtype(dtype)

        if lib.is_np_dtype(dtype, "m"):
            if dtype == self.dtype:
                if copy:
                    return self.copy()
                return self

            if is_supported_unit(get_unit_from_dtype(dtype)):
                # unit conversion e.g. timedelta64[s]
                res_values = astype_overflowsafe(self._ndarray, dtype, copy=False)
                return type(self)._simple_new(
                    res_values, dtype=res_values.dtype, freq=self.freq
                )
            else:
                raise ValueError(
                    f"Cannot convert from {self.dtype} to {dtype}. "
                    "Supported resolutions are 's', 'ms', 'us', 'ns'"
                )

        return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy=copy)
I'm getting this error:

Output:
raise ValueError( ValueError: Cannot convert from timedelta64[ns] to timedelta64[h]. Supported resolutions are 's', 'ms', 'us', 'ns'
When posting error messages please post entire error message and trace. User error tags for error messages.

Your code generates the message. What does this do?
if is_supported_unit(get_unit_from_dtype(dtype)):
I don't think "is_supported_unit()" is a built-in or part of any of the standard libraries. Must be a function in your code. Same goes for get_unit_from_dtype(dtype).