def test_ints_to_pytimedelta_unsupported(unit): arr = np.arange(6, dtype=np.int64).view(f"m8[{unit}]") with pytest.raises(NotImplementedError, match=r"\d{1,2}"): ints_to_pytimedelta(arr, box=False) with pytest.raises(NotImplementedError, match=r"\d{1,2}"): ints_to_pytimedelta(arr, box=True)
def test_ints_to_pytimedelta_unsupported(unit): arr = np.arange(6, dtype=np.int64).view(f"m8[{unit}]") with pytest.raises(NotImplementedError, match=r"\d{1,2}"): ints_to_pytimedelta(arr, box=False) msg = "Only resolutions 's', 'ms', 'us', 'ns' are supported" with pytest.raises(NotImplementedError, match=msg): ints_to_pytimedelta(arr, box=True)
def test_ints_to_pytimedelta(unit): # tests for non-nanosecond cases arr = np.arange(6, dtype=np.int64).view(f"m8[{unit}]") res = ints_to_pytimedelta(arr, box=False) # For non-nanosecond, .astype(object) gives pytimedelta objects # instead of integers expected = arr.astype(object) tm.assert_numpy_array_equal(res, expected) res = ints_to_pytimedelta(arr, box=True) expected = np.array([Timedelta(x) for x in arr], dtype=object) tm.assert_numpy_array_equal(res, expected)
def to_pytimedelta(self) -> npt.NDArray[np.object_]: """ Return an ndarray of datetime.timedelta objects. Returns ------- timedeltas : ndarray[object] """ return ints_to_pytimedelta(self._ndarray)
def __iter__(self): if self.ndim > 1: return (self[n] for n in range(len(self))) else: # convert in chunks of 10k for efficiency data = self.asi8 length = len(self) chunksize = 10000 chunks = int(length / chunksize) + 1 for i in range(chunks): start_i = i * chunksize end_i = min((i + 1) * chunksize, length) converted = ints_to_pytimedelta(data[start_i:end_i], box=True) yield from converted
def __iter__(self): if self.ndim > 1: for i in range(len(self)): yield self[i] else: # convert in chunks of 10k for efficiency data = self._ndarray length = len(self) chunksize = 10000 chunks = (length // chunksize) + 1 for i in range(chunks): start_i = i * chunksize end_i = min((i + 1) * chunksize, length) converted = ints_to_pytimedelta(data[start_i:end_i], box=True) yield from converted