def pd_avg_time_between(x): """Assumes time scales are closer to order of seconds than to nanoseconds if times are much closer to nanoseconds we could get some floating point errors this can be fixed with another function that calculates the mean before converting to seconds """ x = x.dropna() if x.shape[0] < 2: return np.nan if isinstance(x.iloc[0], (pd.Timestamp, datetime)): x = x.astype('int64') # use len(x)-1 because we care about difference # between values, len(x)-1 = len(diff(x)) avg = (x.max() - x.min()) / (len(x) - 1) avg = avg * 1e-9 # long form: # diff_in_ns = x.diff().iloc[1:].astype('int64') # diff_in_seconds = diff_in_ns * 1e-9 # avg = diff_in_seconds.mean() return convert_time_units(avg, self.unit)
def test_convert_time_units(): units = { 'years': 31540000, 'months': 2628000, 'days': 86400, 'hours': 3600, 'minutes': 60, 'seconds': 1, 'milliseconds': 0.001, 'nanoseconds': 0.000000001 } for each in units: assert (convert_time_units(units[each] * 2, each) == 2) assert np.isclose(convert_time_units(float(units[each] * 2), each), 2) error_text = "Invalid unit given, make sure it is plural" with pytest.raises(ValueError, match=error_text): convert_time_units("jnkwjgn", 10)
def test_convert_time_units(): units = { "years": 31540000, "months": 2628000, "days": 86400, "hours": 3600, "minutes": 60, "seconds": 1, "milliseconds": 0.001, "nanoseconds": 0.000000001, } for each in units: assert convert_time_units(units[each] * 2, each) == 2 assert np.isclose(convert_time_units(float(units[each] * 2), each), 2) error_text = "Invalid unit given, make sure it is plural" with pytest.raises(ValueError, match=error_text): convert_time_units("jnkwjgn", 10)
def pd_diff(values): return convert_time_units( values.diff().apply(lambda x: x.total_seconds()), self.unit)
def pd_time_since(array, time): return convert_time_units( (time - pd.DatetimeIndex(array)).total_seconds(), self.unit)
def time_since_first(values, time=None): time_since = time - values.iloc[0] return convert_time_units(time_since.total_seconds(), self.unit)
def pd_time_since(array, time): if isinstance(array, list): array = pd.Series(array) return convert_time_units((time - array).dt.total_seconds(), self.unit)
def pd_time_since(array, time): return convert_time_units((time - array).dt.total_seconds(), self.unit)