def test_dti_shift_tzaware(self, tz_naive_fixture): # GH#9903 tz = tz_naive_fixture idx = DatetimeIndex([], name="xxx", tz=tz) tm.assert_index_equal(idx.shift(0, freq="H"), idx) tm.assert_index_equal(idx.shift(3, freq="H"), idx) idx = DatetimeIndex( ["2011-01-01 10:00", "2011-01-01 11:00", "2011-01-01 12:00"], name="xxx", tz=tz, freq="H", ) tm.assert_index_equal(idx.shift(0, freq="H"), idx) exp = DatetimeIndex( ["2011-01-01 13:00", "2011-01-01 14:00", "2011-01-01 15:00"], name="xxx", tz=tz, freq="H", ) tm.assert_index_equal(idx.shift(3, freq="H"), exp) exp = DatetimeIndex( ["2011-01-01 07:00", "2011-01-01 08:00", "2011-01-01 09:00"], name="xxx", tz=tz, freq="H", ) tm.assert_index_equal(idx.shift(-3, freq="H"), exp)
def test_shift2(self): ts = Series(np.random.randn(5), index=date_range("1/1/2000", periods=5, freq="H")) result = ts.shift(1, freq="5T") exp_index = ts.index.shift(1, freq="5T") tm.assert_index_equal(result.index, exp_index) # GH#1063, multiple of same base result = ts.shift(1, freq="4H") exp_index = ts.index + offsets.Hour(4) tm.assert_index_equal(result.index, exp_index) idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"]) msg = "Cannot shift with no freq" with pytest.raises(NullFrequencyError, match=msg): idx.shift(1)
def test_dti_shift_no_freq(self): # GH#19147 dti = DatetimeIndex(["2011-01-01 10:00", "2011-01-01"], freq=None) with pytest.raises(NullFrequencyError, match="Cannot shift with no freq"): dti.shift(2)
def rolling_speed( dframe: pd.DatetimeIndex, window: int = 3, rounds: int = 3, deriv: int = 1, center: str = None, shift: int = 2, typ: str = "coords", ) -> pd.DataFrame: """ Returns the average speed over n frames in pixels per frame. Args: dframe (pandas.DataFrame): Position over time dataframe. window (int): Number of frames to average over. rounds (int): Float rounding decimals. deriv (int): Position derivative order; 1 for speed, 2 for acceleration, 3 for jerk, etc. center (str): For internal usage only; solves an issue with pandas.MultiIndex that arises when centering frames to a specific body part. shift (int): Window shift for rolling speed calculation. typ (str): Type of dataset. Intended for internal usage only. Returns: speeds (pd.DataFrame): Data frame containing 2D speeds for each body part in the original data or their consequent derivatives. """ original_shape = dframe.shape if center: body_parts = [bp for bp in dframe.columns.levels[0] if bp != center] else: try: body_parts = dframe.columns.levels[0] except AttributeError: body_parts = dframe.columns speeds = pd.DataFrame for der in range(deriv): features = 2 if der == 0 and typ == "coords" else 1 distances = (np.concatenate( [ np.array(dframe).reshape([-1, features], order="C"), np.array(dframe.shift(shift)).reshape([-1, features], order="C"), ], axis=1, ) / shift) distances = np.array(compute_dist(distances)) distances = distances.reshape( [ original_shape[0], (original_shape[1] // 2 if typ == "coords" else original_shape[1]), ], order="C", ) distances = pd.DataFrame(distances, index=dframe.index) speeds = np.round(distances.rolling(window).mean(), rounds) dframe = speeds speeds.columns = body_parts return speeds.fillna(0.0)
def test_tdi_add_sub_float(self, op, other): dti = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') tdi = dti - dti.shift(1) with pytest.raises(TypeError): op(tdi, other)