Python Forum
Cannot convert from timedelta64[ns] to timedelta64[h]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot convert from timedelta64[ns] to timedelta64[h]
#1
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'
Reply
#2
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).
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020