Example #1
0
    def test_interp_alt_scipy(self):
        tm._skip_if_no_scipy()
        df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
                        'C': [1, 2, 3, 5, 8, 13, 21]})
        result = df.interpolate(method='barycentric')
        expected = df.copy()
        expected.loc[2, 'A'] = 3
        expected.loc[5, 'A'] = 6
        assert_frame_equal(result, expected)

        result = df.interpolate(method='barycentric', downcast='infer')
        assert_frame_equal(result, expected.astype(np.int64))

        result = df.interpolate(method='krogh')
        expectedk = df.copy()
        expectedk['A'] = expected['A']
        assert_frame_equal(result, expectedk)

        _skip_if_no_pchip()
        import scipy
        result = df.interpolate(method='pchip')
        expected.loc[2, 'A'] = 3

        if LooseVersion(scipy.__version__) >= '0.17.0':
            expected.loc[5, 'A'] = 6.0
        else:
            expected.loc[5, 'A'] = 6.125

        assert_frame_equal(result, expected)
Example #2
0
 def test_interp_bad_method(self):
     df = DataFrame({'A': [1, 2, np.nan, 4],
                     'B': [1, 4, 9, np.nan],
                     'C': [1, 2, 3, 5],
                     'D': list('abcd')})
     with pytest.raises(ValueError):
         df.interpolate(method='not_a_method')
Example #3
0
    def test_interp_alt_scipy(self):
        tm._skip_if_no_scipy()
        df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
                        'C': [1, 2, 3, 5, 8, 13, 21]})
        result = df.interpolate(method='barycentric')
        expected = df.copy()
        expected['A'].iloc[2] = 3
        expected['A'].iloc[5] = 6
        assert_frame_equal(result, expected)

        result = df.interpolate(method='barycentric', downcast='infer')
        assert_frame_equal(result, expected.astype(np.int64))

        result = df.interpolate(method='krogh')
        expectedk = df.copy()
        # expectedk['A'].iloc[2] = 3
        # expectedk['A'].iloc[5] = 6
        expectedk['A'] = expected['A']
        assert_frame_equal(result, expectedk)

        _skip_if_no_pchip()
        result = df.interpolate(method='pchip')
        expected['A'].iloc[2] = 3
        expected['A'].iloc[5] = 6.125
        assert_frame_equal(result, expected)
Example #4
0
 def test_interp_raise_on_only_mixed(self):
     df = DataFrame({'A': [1, 2, np.nan, 4],
                     'B': ['a', 'b', 'c', 'd'],
                     'C': [np.nan, 2, 5, 7],
                     'D': [np.nan, np.nan, 9, 9],
                     'E': [1, 2, 3, 4]})
     with pytest.raises(TypeError):
         df.interpolate(axis=1)
Example #5
0
 def test_interp_inplace_row(self):
     # GH 10395
     result = DataFrame({'a': [1., 2., 3., 4.],
                         'b': [np.nan, 2., 3., 4.],
                         'c': [3, 2, 2, 2]})
     expected = result.interpolate(method='linear', axis=1, inplace=False)
     result.interpolate(method='linear', axis=1, inplace=True)
     assert_frame_equal(result, expected)
Example #6
0
    def test_interp_leading_nans(self):
        df = DataFrame({"A": [np.nan, np.nan, .5, .25, 0],
                        "B": [np.nan, -3, -3.5, np.nan, -4]})
        result = df.interpolate()
        expected = df.copy()
        expected['B'].loc[3] = -3.75
        assert_frame_equal(result, expected)

        tm._skip_if_no_scipy()
        result = df.interpolate(method='polynomial', order=1)
        assert_frame_equal(result, expected)
Example #7
0
 def test_interp_raise_on_all_object_dtype(self):
     # GH 22985
     df = DataFrame({
         'A': [1, 2, 3],
         'B': [4, 5, 6]},
         dtype='object')
     msg = ("Cannot interpolate with all object-dtype columns "
            "in the DataFrame. Try setting at least one "
            "column to a numeric dtype.")
     with pytest.raises(TypeError, match=msg):
         df.interpolate()
Example #8
0
 def test_interp_raise_on_all_object_dtype(self):
     # GH 22985
     df = DataFrame({
         'A': [1, 2, 3],
         'B': [4, 5, 6]},
         dtype='object')
     with tm.assert_raises_regex(
             TypeError,
             "Cannot interpolate with all object-dtype columns "
             "in the DataFrame. Try setting at least one "
             "column to a numeric dtype."):
         df.interpolate()
 def __call__(self, strategy):
     prices = strategy.get_indicator_prices()
     trend = DataFrame(None, index = prices.index, columns = prices.columns, dtype = float)
     last_SP = Series(None, index = prices.columns)
     current_trend = Series('-', index = prices.columns)
     for i in range(prices.shape[0] - self.period):
         # If there are not any new highs in the recent period then must have been 
         # a swing point high.
         SPH = ~(prices.iloc[(i + 1):(i + self.period)] > prices.iloc[i]).any()
         # NaN in series will produce false signals and need to be removed
         SPH = SPH[prices.iloc[i].notnull()]
         SPH = SPH[SPH]
         # Only mark as swing point high if currently in uptrend or unidentified trend, otherwise ignore.
         SPH = SPH[current_trend[SPH.index] != 'DOWN']
         if not SPH.empty:
             current_trend[SPH.index] = 'DOWN'
             trend.loc[trend.index[i], SPH.index] = prices.iloc[i][SPH.index]
         # Repeat for swing point lows.
         SPL = ~(prices.iloc[(i + 1):(i + self.period)] < prices.iloc[i]).any()
         SPL = SPL[prices.iloc[i].notnull()]
         SPL = SPL[SPL]
         SPL = SPL[current_trend[SPL.index] != 'UP']
         if not SPL.empty:
             current_trend[SPL.index] = 'UP'
             trend.loc[trend.index[i], SPL.index] = prices.iloc[i][SPL.index]
     self.trend = trend.interpolate()
Example #10
0
    def test_interp_rowwise(self):
        df = DataFrame({0: [1, 2, np.nan, 4],
                        1: [2, 3, 4, np.nan],
                        2: [np.nan, 4, 5, 6],
                        3: [4, np.nan, 6, 7],
                        4: [1, 2, 3, 4]})
        result = df.interpolate(axis=1)
        expected = df.copy()
        expected.loc[3, 1] = 5
        expected.loc[0, 2] = 3
        expected.loc[1, 3] = 3
        expected[4] = expected[4].astype(np.float64)
        assert_frame_equal(result, expected)

        result = df.interpolate(axis=1, method='values')
        assert_frame_equal(result, expected)

        result = df.interpolate(axis=0)
        expected = df.interpolate()
        assert_frame_equal(result, expected)
Example #11
0
    def test_interp_basic(self):
        df = DataFrame({'A': [1, 2, np.nan, 4], 'B': [1, 4, 9, np.nan],
                        'C': [1, 2, 3, 5], 'D': list('abcd')})
        expected = DataFrame({'A': [1., 2., 3., 4.], 'B': [1., 4., 9., 9.],
                              'C': [1, 2, 3, 5], 'D': list('abcd')})
        result = df.interpolate()
        assert_frame_equal(result, expected)

        result = df.set_index('C').interpolate()
        expected = df.set_index('C')
        expected.A.loc[3] = 3
        expected.B.loc[5] = 9
        assert_frame_equal(result, expected)
Example #12
0
class Interpolate(object):

    params = [None, 'infer']
    param_names = ['downcast']

    def setup(self, downcast):
        N = 10000
        # this is the worst case, where every column has NaNs.
        self.df = DataFrame(np.random.randn(N, 100))
        self.df.values[::2] = np.nan

        self.df2 = DataFrame({'A': np.arange(0, N),
                              'B': np.random.randint(0, 100, N),
                              'C': np.random.randn(N),
                              'D': np.random.randn(N)})
        self.df2.loc[1::5, 'A'] = np.nan
        self.df2.loc[1::5, 'C'] = np.nan

    def time_interpolate(self, downcast):
        self.df.interpolate(downcast=downcast)

    def time_interpolate_some_good(self, downcast):
        self.df2.interpolate(downcast=downcast)
Example #13
0
    def test_interp_basic(self):
        df = DataFrame({'A': [1, 2, np.nan, 4], 'B': [1, 4, 9, np.nan],
                        'C': [1, 2, 3, 5], 'D': list('abcd')})
        expected = DataFrame({'A': [1, 2, 3, 4], 'B': [1, 4, 9, 9],
                              'C': [1, 2, 3, 5], 'D': list('abcd')})
        result = df.interpolate()
        assert_frame_equal(result, expected)

        result = df.set_index('C').interpolate()
        expected = df.set_index('C')
        expected.A.loc[3] = 3
        expected.B.loc[5] = 9
        expected[['A', 'B']] = expected[['A', 'B']].astype(np.int64)

        assert_frame_equal(result, expected)
Example #14
0
    def test_interp_ignore_all_good(self):
        # GH
        df = DataFrame({'A': [1, 2, np.nan, 4],
                        'B': [1, 2, 3, 4],
                        'C': [1., 2., np.nan, 4.],
                        'D': [1., 2., 3., 4.]})
        expected = DataFrame({'A': np.array([1, 2, 3, 4], dtype='float64'),
                              'B': np.array([1, 2, 3, 4], dtype='int64'),
                              'C': np.array([1., 2., 3, 4.], dtype='float64'),
                              'D': np.array([1., 2., 3., 4.], dtype='float64')})

        result = df.interpolate(downcast=None)
        assert_frame_equal(result, expected)

        # all good
        result = df[['B', 'D']].interpolate(downcast=None)
        assert_frame_equal(result, df[['B', 'D']])
Example #15
0
def test_interpolate():
    skip_if_not_available(modules=['pandas'])
    """ Ensure tha DataFrame.interpolate(method='nearest') has the
    desired properties.

    It is used by blocks-plot and should:

    * interpolate missing/NaN datapoints between valid ones
    * not replace any NaN before/after the first/last finite datapoint
    """
    y = [nan, nan, 2., 3., nan, 5, nan, nan]
    df = DataFrame(y)
    df_ = df.interpolate(method='nearest')[0]

    assert all(isfinite(df_[2:6]))
    assert all(~isfinite(df_[0:2]))
    assert all(~isfinite(df_[6:8]))
Example #16
0
    def test_interp_various(self):
        tm._skip_if_no_scipy()

        df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
                        'C': [1, 2, 3, 5, 8, 13, 21]})
        df = df.set_index('C')
        expected = df.copy()
        result = df.interpolate(method='polynomial', order=1)

        expected.A.loc[3] = 2.66666667
        expected.A.loc[13] = 5.76923076
        assert_frame_equal(result, expected)

        result = df.interpolate(method='cubic')
        # GH #15662.
        # new cubic and quadratic interpolation algorithms from scipy 0.19.0.
        # previously `splmake` was used. See scipy/scipy#6710
        if _is_scipy_ge_0190:
            expected.A.loc[3] = 2.81547781
            expected.A.loc[13] = 5.52964175
        else:
            expected.A.loc[3] = 2.81621174
            expected.A.loc[13] = 5.64146581
        assert_frame_equal(result, expected)

        result = df.interpolate(method='nearest')
        expected.A.loc[3] = 2
        expected.A.loc[13] = 5
        assert_frame_equal(result, expected, check_dtype=False)

        result = df.interpolate(method='quadratic')
        if _is_scipy_ge_0190:
            expected.A.loc[3] = 2.82150771
            expected.A.loc[13] = 6.12648668
        else:
            expected.A.loc[3] = 2.82533638
            expected.A.loc[13] = 6.02817974
        assert_frame_equal(result, expected)

        result = df.interpolate(method='slinear')
        expected.A.loc[3] = 2.66666667
        expected.A.loc[13] = 5.76923077
        assert_frame_equal(result, expected)

        result = df.interpolate(method='zero')
        expected.A.loc[3] = 2.
        expected.A.loc[13] = 5
        assert_frame_equal(result, expected, check_dtype=False)
Example #17
0
    def test_interp_various(self):
        _skip_if_no_scipy()
        df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
                        'C': [1, 2, 3, 5, 8, 13, 21]})
        df = df.set_index('C')
        expected = df.copy()
        result = df.interpolate(method='polynomial', order=1)

        expected.A.loc[3] = 2.66666667
        expected.A.loc[13] = 5.76923076
        assert_frame_equal(result, expected)

        result = df.interpolate(method='cubic')
        expected.A.loc[3] = 2.81621174
        expected.A.loc[13] = 5.64146581
        assert_frame_equal(result, expected)

        result = df.interpolate(method='nearest')
        expected.A.loc[3] = 2
        expected.A.loc[13] = 5
        assert_frame_equal(result, expected, check_dtype=False)

        result = df.interpolate(method='quadratic')
        expected.A.loc[3] = 2.82533638
        expected.A.loc[13] = 6.02817974
        assert_frame_equal(result, expected)

        result = df.interpolate(method='slinear')
        expected.A.loc[3] = 2.66666667
        expected.A.loc[13] = 5.76923077
        assert_frame_equal(result, expected)

        result = df.interpolate(method='zero')
        expected.A.loc[3] = 2.
        expected.A.loc[13] = 5
        assert_frame_equal(result, expected, check_dtype=False)

        result = df.interpolate(method='quadratic')
        expected.A.loc[3] = 2.82533638
        expected.A.loc[13] = 6.02817974
        assert_frame_equal(result, expected)
Example #18
0
    def test_interp_various(self):
        df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
                        'C': [1, 2, 3, 5, 8, 13, 21]})
        df = df.set_index('C')
        expected = df.copy()
        result = df.interpolate(method='polynomial', order=1)

        expected.A.loc[3] = 2.66666667
        expected.A.loc[13] = 5.76923076
        assert_frame_equal(result, expected)

        result = df.interpolate(method='cubic')
        # GH #15662.
        expected.A.loc[3] = 2.81547781
        expected.A.loc[13] = 5.52964175
        assert_frame_equal(result, expected)

        result = df.interpolate(method='nearest')
        expected.A.loc[3] = 2
        expected.A.loc[13] = 5
        assert_frame_equal(result, expected, check_dtype=False)

        result = df.interpolate(method='quadratic')
        expected.A.loc[3] = 2.82150771
        expected.A.loc[13] = 6.12648668
        assert_frame_equal(result, expected)

        result = df.interpolate(method='slinear')
        expected.A.loc[3] = 2.66666667
        expected.A.loc[13] = 5.76923077
        assert_frame_equal(result, expected)

        result = df.interpolate(method='zero')
        expected.A.loc[3] = 2.
        expected.A.loc[13] = 5
        assert_frame_equal(result, expected, check_dtype=False)
Example #19
0
 def test_rowwise_alt(self):
     df = DataFrame({0: [0, .5, 1., np.nan, 4, 8, np.nan, np.nan, 64],
                     1: [1, 2, 3, 4, 3, 2, 1, 0, -1]})
     df.interpolate(axis=0)
Example #20
0
 def test_interp_nan_idx(self):
     df = DataFrame({'A': [1, 2, np.nan, 4], 'B': [np.nan, 2, 3, 4]})
     df = df.set_index('A')
     with pytest.raises(NotImplementedError):
         df.interpolate(method='values')