Python scipy.sparse.shift() Examples

The following are 24 code examples of scipy.sparse.shift(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.sparse , or try the search function .
Example #1
Source File: test_series.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_shift(self):
        series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))

        shifted = series.shift(0)
        assert shifted is not series
        tm.assert_sp_series_equal(shifted, series)

        f = lambda s: s.shift(1)
        _dense_series_compare(series, f)

        f = lambda s: s.shift(-2)
        _dense_series_compare(series, f)

        series = SparseSeries([nan, 1., 2., 3., nan, nan],
                              index=bdate_range('1/1/2000', periods=6))
        f = lambda s: s.shift(2, freq='B')
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, freq=BDay())
        _dense_series_compare(series, f) 
Example #2
Source File: test_series.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_shift_dtype_fill_value(self):
        # GH 12908
        orig = pd.Series([1, 0, 0, 4], dtype=np.int64)

        for v in [0, 1, np.nan]:
            sparse = orig.to_sparse(fill_value=v)

            tm.assert_sp_series_equal(sparse.shift(0),
                                      orig.shift(0).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(1),
                                      orig.shift(1).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(2),
                                      orig.shift(2).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(3),
                                      orig.shift(3).to_sparse(fill_value=v))

            tm.assert_sp_series_equal(sparse.shift(-1),
                                      orig.shift(-1).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-2),
                                      orig.shift(-2).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-3),
                                      orig.shift(-3).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-4),
                                      orig.shift(-4).to_sparse(fill_value=v)) 
Example #3
Source File: test_series.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_shift_dtype(self):
        # GH 12908
        orig = pd.Series([1, 2, 3, 4], dtype=np.int64)

        sparse = orig.to_sparse()
        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())

        sparse = orig.to_sparse(fill_value=np.nan)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=np.nan))
        # shift(1) or more span changes dtype to float64
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse()) 
Example #4
Source File: test_series.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_shift(self):
        series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))

        shifted = series.shift(0)
        assert shifted is not series
        tm.assert_sp_series_equal(shifted, series)

        f = lambda s: s.shift(1)
        _dense_series_compare(series, f)

        f = lambda s: s.shift(-2)
        _dense_series_compare(series, f)

        series = SparseSeries([nan, 1., 2., 3., nan, nan],
                              index=bdate_range('1/1/2000', periods=6))
        f = lambda s: s.shift(2, freq='B')
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, freq=BDay())
        _dense_series_compare(series, f) 
Example #5
Source File: test_series.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_shift(self):
        series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))

        shifted = series.shift(0)
        # assert shifted is not series
        tm.assert_sp_series_equal(shifted, series)

        f = lambda s: s.shift(1)
        _dense_series_compare(series, f)

        f = lambda s: s.shift(-2)
        _dense_series_compare(series, f)

        series = SparseSeries([nan, 1., 2., 3., nan, nan],
                              index=bdate_range('1/1/2000', periods=6))
        f = lambda s: s.shift(2, freq='B')
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, freq=BDay())
        _dense_series_compare(series, f) 
Example #6
Source File: test_series.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_shift_dtype_fill_value(self):
        # GH 12908
        orig = pd.Series([1, 0, 0, 4], dtype=np.int64)

        for v in [0, 1, np.nan]:
            sparse = orig.to_sparse(fill_value=v)

            tm.assert_sp_series_equal(sparse.shift(0),
                                      orig.shift(0).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(1),
                                      orig.shift(1).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(2),
                                      orig.shift(2).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(3),
                                      orig.shift(3).to_sparse(fill_value=v))

            tm.assert_sp_series_equal(sparse.shift(-1),
                                      orig.shift(-1).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-2),
                                      orig.shift(-2).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-3),
                                      orig.shift(-3).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-4),
                                      orig.shift(-4).to_sparse(fill_value=v)) 
Example #7
Source File: test_series.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_shift_dtype(self):
        # GH 12908
        orig = pd.Series([1, 2, 3, 4], dtype=np.int64)

        sparse = orig.to_sparse()
        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())

        sparse = orig.to_sparse(fill_value=np.nan)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=np.nan))
        # shift(1) or more span changes dtype to float64
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse()) 
Example #8
Source File: test_series.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_shift(self):
        series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))

        shifted = series.shift(0)
        # assert shifted is not series
        tm.assert_sp_series_equal(shifted, series)

        f = lambda s: s.shift(1)
        _dense_series_compare(series, f)

        f = lambda s: s.shift(-2)
        _dense_series_compare(series, f)

        series = SparseSeries([nan, 1., 2., 3., nan, nan],
                              index=bdate_range('1/1/2000', periods=6))
        f = lambda s: s.shift(2, freq='B')
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, freq=BDay())
        _dense_series_compare(series, f) 
Example #9
Source File: test_series.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_shift(self):
        series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))

        shifted = series.shift(0)
        # assert shifted is not series
        tm.assert_sp_series_equal(shifted, series)

        f = lambda s: s.shift(1)
        _dense_series_compare(series, f)

        f = lambda s: s.shift(-2)
        _dense_series_compare(series, f)

        series = SparseSeries([nan, 1., 2., 3., nan, nan],
                              index=bdate_range('1/1/2000', periods=6))
        f = lambda s: s.shift(2, freq='B')
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, freq=BDay())
        _dense_series_compare(series, f) 
Example #10
Source File: test_series.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_shift_dtype_fill_value(self):
        # GH 12908
        orig = pd.Series([1, 0, 0, 4], dtype=np.int64)

        for v in [0, 1, np.nan]:
            sparse = orig.to_sparse(fill_value=v)

            tm.assert_sp_series_equal(sparse.shift(0),
                                      orig.shift(0).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(1),
                                      orig.shift(1).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(2),
                                      orig.shift(2).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(3),
                                      orig.shift(3).to_sparse(fill_value=v))

            tm.assert_sp_series_equal(sparse.shift(-1),
                                      orig.shift(-1).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-2),
                                      orig.shift(-2).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-3),
                                      orig.shift(-3).to_sparse(fill_value=v))
            tm.assert_sp_series_equal(sparse.shift(-4),
                                      orig.shift(-4).to_sparse(fill_value=v)) 
Example #11
Source File: test_series.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_shift_dtype(self):
        # GH 12908
        orig = pd.Series([1, 2, 3, 4], dtype=np.int64)

        sparse = orig.to_sparse()
        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())

        sparse = orig.to_sparse(fill_value=np.nan)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=np.nan))
        # shift(1) or more span changes dtype to float64
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse()) 
Example #12
Source File: test_series.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_shift(self):
        series = SparseSeries([nan, 1., 2., 3., nan, nan], index=np.arange(6))

        shifted = series.shift(0)
        assert shifted is not series
        tm.assert_sp_series_equal(shifted, series)

        f = lambda s: s.shift(1)
        _dense_series_compare(series, f)

        f = lambda s: s.shift(-2)
        _dense_series_compare(series, f)

        series = SparseSeries([nan, 1., 2., 3., nan, nan],
                              index=bdate_range('1/1/2000', periods=6))
        f = lambda s: s.shift(2, freq='B')
        _dense_series_compare(series, f)

        f = lambda s: s.shift(2, freq=BDay())
        _dense_series_compare(series, f) 
Example #13
Source File: test_series.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_shift_dtype(self):
        # GH 12908
        orig = pd.Series([1, 2, 3, 4], dtype=np.int64)

        sparse = orig.to_sparse()
        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())

        sparse = orig.to_sparse(fill_value=np.nan)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=np.nan))
        # shift(1) or more span changes dtype to float64
        # XXX: SparseSeries doesn't need to shift dtype here.
        # Do we want to astype in shift, for backwards compat?
        # If not, document it.
        tm.assert_sp_series_equal(sparse.shift(1).astype('f8'),
                                  orig.shift(1).to_sparse(kind='integer'))
        tm.assert_sp_series_equal(sparse.shift(2).astype('f8'),
                                  orig.shift(2).to_sparse(kind='integer'))
        tm.assert_sp_series_equal(sparse.shift(3).astype('f8'),
                                  orig.shift(3).to_sparse(kind='integer'))

        tm.assert_sp_series_equal(sparse.shift(-1).astype('f8'),
                                  orig.shift(-1).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-2).astype('f8'),
                                  orig.shift(-2).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-3).astype('f8'),
                                  orig.shift(-3).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-4).astype('f8'),
                                  orig.shift(-4).to_sparse(),
                                  check_kind=False) 
Example #14
Source File: test_series.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_shift_dtype_fill_value(self, fill_value, periods):
        # GH 12908
        orig = pd.Series([1, 0, 0, 4], dtype=np.dtype('int64'))

        sparse = orig.to_sparse(fill_value=fill_value)

        result = sparse.shift(periods)
        expected = orig.shift(periods).to_sparse(fill_value=fill_value)

        tm.assert_sp_series_equal(result, expected,
                                  check_kind=False,
                                  consolidate_block_indices=True) 
Example #15
Source File: test_series.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_shift_nan(self):
        # GH 12908
        orig = pd.Series([np.nan, 2, np.nan, 4, 0, np.nan, 0])
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

        sparse = orig.to_sparse(fill_value=0)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0))

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0)) 
Example #16
Source File: test_series.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_shift_nan(self):
        # GH 12908
        orig = pd.Series([np.nan, 2, np.nan, 4, 0, np.nan, 0])
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

        sparse = orig.to_sparse(fill_value=0)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0))

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0)) 
Example #17
Source File: test_series.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_shift_dtype(self):
        # GH 12908
        orig = pd.Series([1, 2, 3, 4], dtype=np.int64)

        sparse = orig.to_sparse()
        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())

        sparse = orig.to_sparse(fill_value=np.nan)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=np.nan))
        # shift(1) or more span changes dtype to float64
        # XXX: SparseSeries doesn't need to shift dtype here.
        # Do we want to astype in shift, for backwards compat?
        # If not, document it.
        tm.assert_sp_series_equal(sparse.shift(1).astype('f8'),
                                  orig.shift(1).to_sparse(kind='integer'))
        tm.assert_sp_series_equal(sparse.shift(2).astype('f8'),
                                  orig.shift(2).to_sparse(kind='integer'))
        tm.assert_sp_series_equal(sparse.shift(3).astype('f8'),
                                  orig.shift(3).to_sparse(kind='integer'))

        tm.assert_sp_series_equal(sparse.shift(-1).astype('f8'),
                                  orig.shift(-1).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-2).astype('f8'),
                                  orig.shift(-2).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-3).astype('f8'),
                                  orig.shift(-3).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-4).astype('f8'),
                                  orig.shift(-4).to_sparse(),
                                  check_kind=False) 
Example #18
Source File: test_series.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_shift_dtype_fill_value(self, fill_value, periods):
        # GH 12908
        orig = pd.Series([1, 0, 0, 4], dtype=np.dtype('int64'))

        sparse = orig.to_sparse(fill_value=fill_value)

        result = sparse.shift(periods)
        expected = orig.shift(periods).to_sparse(fill_value=fill_value)

        tm.assert_sp_series_equal(result, expected,
                                  check_kind=False,
                                  consolidate_block_indices=True) 
Example #19
Source File: test_series.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shift_dtype_fill_value(self, fill_value, periods):
        # GH 12908
        orig = pd.Series([1, 0, 0, 4], dtype=np.dtype('int64'))

        sparse = orig.to_sparse(fill_value=fill_value)

        result = sparse.shift(periods)
        expected = orig.shift(periods).to_sparse(fill_value=fill_value)

        tm.assert_sp_series_equal(result, expected,
                                  check_kind=False,
                                  consolidate_block_indices=True) 
Example #20
Source File: test_series.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_shift_nan(self):
        # GH 12908
        orig = pd.Series([np.nan, 2, np.nan, 4, 0, np.nan, 0])
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse())

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

        sparse = orig.to_sparse(fill_value=0)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0))

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0))
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0)) 
Example #21
Source File: test_series.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_shift_dtype(self):
        # GH 12908
        orig = pd.Series([1, 2, 3, 4], dtype=np.int64)

        sparse = orig.to_sparse()
        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse())

        sparse = orig.to_sparse(fill_value=np.nan)
        tm.assert_sp_series_equal(sparse.shift(0),
                                  orig.shift(0).to_sparse(fill_value=np.nan))
        # shift(1) or more span changes dtype to float64
        # XXX: SparseSeries doesn't need to shift dtype here.
        # Do we want to astype in shift, for backwards compat?
        # If not, document it.
        tm.assert_sp_series_equal(sparse.shift(1).astype('f8'),
                                  orig.shift(1).to_sparse(kind='integer'))
        tm.assert_sp_series_equal(sparse.shift(2).astype('f8'),
                                  orig.shift(2).to_sparse(kind='integer'))
        tm.assert_sp_series_equal(sparse.shift(3).astype('f8'),
                                  orig.shift(3).to_sparse(kind='integer'))

        tm.assert_sp_series_equal(sparse.shift(-1).astype('f8'),
                                  orig.shift(-1).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-2).astype('f8'),
                                  orig.shift(-2).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-3).astype('f8'),
                                  orig.shift(-3).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-4).astype('f8'),
                                  orig.shift(-4).to_sparse(),
                                  check_kind=False) 
Example #22
Source File: test_series.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 4 votes vote down vote up
def test_shift_nan(self):
        # GH 12908
        orig = pd.Series([np.nan, 2, np.nan, 4, 0, np.nan, 0])
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse(),
                                  check_kind=False)

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

        sparse = orig.to_sparse(fill_value=0)
        tm.assert_sp_series_equal(
            sparse.shift(0),
            orig.shift(0).to_sparse(fill_value=sparse.fill_value)
        )
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0),
                                  check_kind=False)

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0),
                                  check_kind=False) 
Example #23
Source File: test_series.py    From coffeegrindsize with MIT License 4 votes vote down vote up
def test_shift_nan(self):
        # GH 12908
        orig = pd.Series([np.nan, 2, np.nan, 4, 0, np.nan, 0])
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse(),
                                  check_kind=False)

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

        sparse = orig.to_sparse(fill_value=0)
        tm.assert_sp_series_equal(
            sparse.shift(0),
            orig.shift(0).to_sparse(fill_value=sparse.fill_value)
        )
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0),
                                  check_kind=False)

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0),
                                  check_kind=False) 
Example #24
Source File: test_series.py    From recruit with Apache License 2.0 4 votes vote down vote up
def test_shift_nan(self):
        # GH 12908
        orig = pd.Series([np.nan, 2, np.nan, 4, 0, np.nan, 0])
        sparse = orig.to_sparse()

        tm.assert_sp_series_equal(sparse.shift(0), orig.shift(0).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(1), orig.shift(1).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(2), orig.shift(2).to_sparse(),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(3), orig.shift(3).to_sparse(),
                                  check_kind=False)

        tm.assert_sp_series_equal(sparse.shift(-1), orig.shift(-1).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-2), orig.shift(-2).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-3), orig.shift(-3).to_sparse())
        tm.assert_sp_series_equal(sparse.shift(-4), orig.shift(-4).to_sparse())

        sparse = orig.to_sparse(fill_value=0)
        tm.assert_sp_series_equal(
            sparse.shift(0),
            orig.shift(0).to_sparse(fill_value=sparse.fill_value)
        )
        tm.assert_sp_series_equal(sparse.shift(1),
                                  orig.shift(1).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(2),
                                  orig.shift(2).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(3),
                                  orig.shift(3).to_sparse(fill_value=0),
                                  check_kind=False)

        tm.assert_sp_series_equal(sparse.shift(-1),
                                  orig.shift(-1).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-2),
                                  orig.shift(-2).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-3),
                                  orig.shift(-3).to_sparse(fill_value=0),
                                  check_kind=False)
        tm.assert_sp_series_equal(sparse.shift(-4),
                                  orig.shift(-4).to_sparse(fill_value=0),
                                  check_kind=False)