def test_mask_neginf_to_posinf_with_nan_initial(initial_value, tuple_mask): mask_ = sc.Stairs(end=-4) nan_initial = s1().mask(mask_) mask_arg = (None, None) if tuple_mask else sc.Stairs(initial_value=initial_value) s = nan_initial.mask(mask_arg) assert np.isnan(s.initial_value) assert len(s.step_points) == 0
def test_slice_month_period_range(freq_str): # GH108 date_freq = "MS" if freq_str == "M" else freq_str pr = pd.period_range("2020", periods=100, freq=freq_str) dr = pd.date_range("2020", periods=100, freq=date_freq) slicer1 = sc.Stairs().slice(pr) slicer2 = sc.Stairs().slice(dr) slicer1._create_slices() slicer2._create_slices() assert all([s1.identical(s2) for s1, s2, in zip(slicer1._slices, slicer2._slices)])
def test_mask_posinf_with_nan_initial(start, expected_step_values, tuple_mask): mask_ = (None, -4) if tuple_mask else sc.Stairs(end=-4) nan_initial = s1().mask(mask_) mask_ = (start, None) if tuple_mask else sc.Stairs(start=start) s = nan_initial.mask(mask_) pd.testing.assert_series_equal( s.step_values, expected_step_values, check_names=False, check_index_type=False, ) assert np.isnan(s.initial_value)
def _sanitize_binary_operands(self, other, copy_other=False): if not isinstance(self, sc.Stairs): self = sc.Stairs(initial_value=self, closed=other.closed) else: self = self.copy() if not isinstance(other, sc.Stairs): other = sc.Stairs(initial_value=other, closed=self.closed) else: if copy_other: other = other.copy() return self, other
def test_where_neginf_with_nan_initial(end, expected_step_values, tuple_mask): mask_ = (None, -4) if tuple_mask else sc.Stairs(end=-4) nan_initial = s1().mask(mask_) mask_ = (None, end) if tuple_mask else sc.Stairs(end=end) s = nan_initial.where(mask_) pd.testing.assert_series_equal( s.step_values, expected_step_values, check_names=False, check_index_type=False, ) assert np.isnan(s.initial_value)
def s2(date_func): return (sc.Stairs().layer( start=timestamp(2019, 12, 29, date_func=date_func), value=-1.75).layer( start=timestamp( 2020, 1, 1, date_func=date_func), value=-0.75).layer( start=timestamp(2020, 1, 2, date_func=date_func), value=4.5).layer( start=timestamp(2020, 1, 2, 12, date_func=date_func), value=-2.5).layer( start=timestamp(2020, 1, 7, date_func=date_func), value=2.5).layer( start=timestamp( 2020, 1, 8, date_func=date_func), value=-4.5).layer( start=timestamp( 2020, 1, 10, date_func=date_func), value=2.5).layer( start=timestamp(2020, 1, 11, date_func=date_func), value=5).layer(start=timestamp( 2020, 1, 13, date_func=date_func), value=-5))
def data(): """ Length-100 array for this type. * data[0] and data[1] should both be non missing * data[0] and data[1] should not be equal """ return sc.StairsArray([sc.Stairs().layer(0, i + 1) for i in range(100)])
def s1(initial_value=0): int_seq1 = sc.Stairs(initial_value=initial_value) int_seq1.layer(1, 10, 2) int_seq1.layer(-4, 5, -1.75) int_seq1.layer(3, 5, 2.5) int_seq1.layer(6, 7, -2.5) int_seq1.layer(7, 10, -2.5) return int_seq1
def s1(closed="left"): int_seq1 = sc.Stairs(initial_value=0, closed=closed) int_seq1.layer(1, 10, 2) int_seq1.layer(-4, 5, -1.75) int_seq1.layer(3, 5, 2.5) int_seq1.layer(6, 7, -2.5) int_seq1.layer(7, 10, -2.5) return int_seq1
def s2(initial_value=0): int_seq2 = sc.Stairs(initial_value=initial_value) int_seq2.layer(1, 7, -2.5) int_seq2.layer(8, 10, 5) int_seq2.layer(2, 5, 4.5) int_seq2.layer(2.5, 4, -2.5) int_seq2.layer(-2, 1, -1.75) return int_seq2
def s2(closed="left"): int_seq2 = sc.Stairs(initial_value=0, closed=closed) int_seq2.layer(1, 7, -2.5) int_seq2.layer(8, 10, 5) int_seq2.layer(2, 5, 4.5) int_seq2.layer(2.5, 4, -2.5) int_seq2.layer(-2, 1, -1.75) return int_seq2
def test_mask_neginf(end, expected_step_values): s = s1().mask(sc.Stairs(end=end)) pd.testing.assert_series_equal( s.step_values, expected_step_values, check_names=False, check_index_type=False, ) assert np.isnan(s.initial_value)
def test_mask_posinf(start, expected_step_values): s = s1().mask(sc.Stairs(start=start)) pd.testing.assert_series_equal( s.step_values, expected_step_values, check_names=False, check_index_type=False, ) assert s.initial_value == 0
def test_where_neginf(end, expected_step_values): s = s1().where(sc.Stairs(end=end)) pd.testing.assert_series_equal( s.step_values, expected_step_values, check_names=False, check_index_type=False, ) assert s.initial_value == 0
def test_where_posinf(start, expected_step_values): s = s1().where(sc.Stairs(start=start)) pd.testing.assert_series_equal( s.step_values, expected_step_values, check_names=False, check_index_type=False, ) assert np.isnan(s.initial_value)
def s2(): return (sc.Stairs().layer(start=-2, value=-1.75).layer( start=1, value=-0.75).layer(start=2, value=4.5).layer( start=2.5, value=-2.5).layer(start=7, value=2.5).layer( start=8, value=-4.5).layer(start=10, value=2.5).layer(start=11, value=5).layer(start=13, value=-5))
def s3(closed="left"): # boolean int_seq = sc.Stairs(initial_value=0, closed=closed) int_seq.layer(-10, 10, 1) int_seq.layer(-8, -7, -1) int_seq.layer(-5, -2, -1) int_seq.layer(0.5, 1, -1) int_seq.layer(3, 3.5, -1) int_seq.layer(7, 9.5, -1) return int_seq
def s4(closed="left"): # boolean int_seq = sc.Stairs(initial_value=0, closed=closed) int_seq.layer(-11, 9, 1) int_seq.layer(-9.5, -8, -1) int_seq.layer(-7.5, -7, -1) int_seq.layer(0, 3, -1) int_seq.layer(6, 6.5, -1) int_seq.layer(7, 8.5, -1) return int_seq
def test_mask_single_step(start, end, expected_step_values, tuple_mask): mask_ = (start, end) if tuple_mask else sc.Stairs(start=start, end=end) s = s1().mask(mask_) pd.testing.assert_series_equal( s.step_values, expected_step_values, check_names=False, check_index_type=False, ) assert s.initial_value == 0
def test_where_on_stepless(): pd.testing.assert_series_equal( sc.Stairs().where((0, 2)).step_changes, pd.Series({ 0: 0.0, 2: np.nan }), check_names=False, check_index_type=False, )
def test_mask_on_stepless(): pd.testing.assert_series_equal( sc.Stairs().mask((0, 2)).step_changes, pd.Series({ 0: np.nan, 2: 0.0 }), check_names=False, check_index_type=False, )
def test_clipped_endpoint(): result = sc.Stairs().clip(0, 1) expected = pd.Series([0.0, np.nan]) pd.testing.assert_series_equal( result.step_values, expected, check_names=False, check_index_type=False, ) assert np.isnan(result.initial_value)
def _mask_stairs(self, other, inverse): def is_full_inverse_mask(initial_value): return initial_value == 0 or np.isnan(initial_value) full_mask_comparator = is_full_inverse_mask if inverse else float(0).__ne__ if other._data is None: if full_mask_comparator(other.initial_value): return sc.Stairs(initial_value=np.nan) else: return self.copy() return _maskify(other, inverse=inverse) + self
def test_mask_make_boolean_s1(kwargs, expected_values, expected_initial): result = s1().mask(sc.Stairs(**kwargs)).make_boolean() pd.testing.assert_series_equal( result.step_values, expected_values, check_names=False, check_index_type=False, ) if np.isnan(expected_initial): assert np.isnan(result.initial_value) else: assert result.initial_value == expected_initial
def func(self): if np.isnan(self.initial_value): initial_value = np.nan else: initial_value = float_comp(self.initial_value, 0) * 1 if self._data is None: return sc.Stairs(initial_value=initial_value, closed=self.closed) values = series_comp(self._get_values(), 0) * 1 values.loc[np.isnan(self._get_values().values)] = np.nan result = sc.Stairs._new( initial_value=initial_value, data=pd.DataFrame({"value": values}, ), closed=self.closed, ) result._remove_redundant_step_points() return result
def data_for_grouping(): """ Data for factorization, grouping, and unique tests. Expected to be like [B, B, NA, NA, A, A, B, C] Where A < B < C and NA is missing """ return sc.StairsArray([ sc.Stairs().layer(1, 3), sc.Stairs().layer(1, 3), None, None, sc.Stairs().layer(2, 4), sc.Stairs().layer(2, 4), sc.Stairs().layer(1, 3), sc.Stairs().layer(1, 4), ])
def handle_tuple_mask(self, left, right): return _mask_stairs(self, sc.Stairs().layer(start=left, end=right), inverse=False)
def data_missing(): """Length-2 array with [NA, Valid]""" return sc.StairsArray([None, sc.Stairs()])
def test_aggregation_with_constant_stairs(func, swap_order): # GH119 - no need to test result, just that it is passing s1 = sc.Stairs().layer(pd.Timestamp("2020"), pd.Timestamp("2021")) s2 = sc.Stairs() arr = [s2, s1] if swap_order else [s1, s2] func(arr)
int_seq2.layer(-2, 1, -1.75) return int_seq2 @pytest.fixture def s1_fix(): return s1(0) # %% @pytest.mark.parametrize( "stairs1, stairs2", [ (s1().mask(sc.Stairs(start=1, end=3)), s2()), (s2().mask(sc.Stairs(start=1, end=3)), s1()), (s1(), s2().mask(sc.Stairs(start=1, end=3))), (s2(), s1().mask(sc.Stairs(start=1, end=3))), (s1().mask(sc.Stairs(start=1, end=3)), s2().mask(sc.Stairs(start=1, end=3))), (s2().mask(sc.Stairs(start=1, end=3)), s1().mask(sc.Stairs(start=1, end=3))), ], ) def test_non_unique_mask_multiplication(stairs1, stairs2): result = stairs1 * stairs2 expected = pd.Series( { -2.0: 3.0625, 1.0: np.nan, 3.0: -1.375, 4.0: 5.5,