Python pandas.core.common.SettingWithCopyError() Examples

The following are 30 code examples of pandas.core.common.SettingWithCopyError(). 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 pandas.core.common , or try the search function .
Example #1
Source File: test_chaining_and_caching.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_setting_with_copy_bug(self):

        # operating on a copy
        df = pd.DataFrame({'a': list(range(4)),
                           'b': list('ab..'),
                           'c': ['a', 'b', np.nan, 'd']})
        mask = pd.isna(df.c)

        def f():
            df[['c']][mask] = df[['b']][mask]

        pytest.raises(com.SettingWithCopyError, f)

        # invalid warning as we are returning a new object
        # GH 8730
        df1 = DataFrame({'x': Series(['a', 'b', 'c']),
                         'y': Series(['d', 'e', 'f'])})
        df2 = df1[['x']]

        # this should not raise
        df2['y'] = ['g', 'h', 'i'] 
Example #2
Source File: test_chaining_and_caching.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_detect_chained_assignment():
    # Inplace ops, originally from:
    # http://stackoverflow.com/questions/20508968/series-fillna-in-a-multiindex-dataframe-does-not-fill-is-this-a-bug
    a = [12, 23]
    b = [123, None]
    c = [1234, 2345]
    d = [12345, 23456]
    tuples = [('eyes', 'left'), ('eyes', 'right'), ('ears', 'left'),
              ('ears', 'right')]
    events = {('eyes', 'left'): a,
              ('eyes', 'right'): b,
              ('ears', 'left'): c,
              ('ears', 'right'): d}
    multiind = MultiIndex.from_tuples(tuples, names=['part', 'side'])
    zed = DataFrame(events, index=['a', 'b'], columns=multiind)

    with pytest.raises(com.SettingWithCopyError):
        zed['eyes']['right'].fillna(value=555, inplace=True) 
Example #3
Source File: test_chaining_and_caching.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_setting_with_copy_bug(self):

        # operating on a copy
        df = DataFrame({'a': list(range(4)),
                        'b': list('ab..'),
                        'c': ['a', 'b', np.nan, 'd']})
        mask = pd.isna(df.c)

        def f():
            df[['c']][mask] = df[['b']][mask]

        pytest.raises(com.SettingWithCopyError, f)

        # invalid warning as we are returning a new object
        # GH 8730
        df1 = DataFrame({'x': Series(['a', 'b', 'c']),
                         'y': Series(['d', 'e', 'f'])})
        df2 = df1[['x']]

        # this should not raise
        df2['y'] = ['g', 'h', 'i'] 
Example #4
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_xs_level(self):
        result = self.frame.xs('two', level='second')
        expected = self.frame[self.frame.index.get_level_values(1) == 'two']
        expected.index = expected.index.droplevel(1)

        tm.assert_frame_equal(result, expected)

        index = MultiIndex.from_tuples([('x', 'y', 'z'), ('a', 'b', 'c'), (
            'p', 'q', 'r')])
        df = DataFrame(np.random.randn(3, 5), index=index)
        result = df.xs('c', level=2)
        expected = df[1:2]
        expected.index = expected.index.droplevel(2)
        tm.assert_frame_equal(result, expected)

        # this is a copy in 0.14
        result = self.frame.xs('two', level='second')

        # setting this will give a SettingWithCopyError
        # as we are trying to write a view
        def f(x):
            x[:] = 10

        pytest.raises(com.SettingWithCopyError, f, result) 
Example #5
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_frame_getitem_view(self):
        df = self.frame.T.copy()

        # this works because we are modifying the underlying array
        # really a no-no
        df['foo'].values[:] = 0
        assert (df['foo'].values == 0).all()

        # but not if it's mixed-type
        df['foo', 'four'] = 'foo'
        df = df.sort_index(level=0, axis=1)

        # this will work, but will raise/warn as its chained assignment
        def f():
            df['foo']['one'] = 2
            return df

        pytest.raises(com.SettingWithCopyError, f)

        try:
            df = f()
        except:
            pass
        assert (df['foo', 'one'] == 0).all() 
Example #6
Source File: test_chaining_and_caching.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_setting_with_copy_bug(self):

        # operating on a copy
        df = DataFrame({'a': list(range(4)),
                        'b': list('ab..'),
                        'c': ['a', 'b', np.nan, 'd']})
        mask = pd.isna(df.c)

        def f():
            df[['c']][mask] = df[['b']][mask]

        pytest.raises(com.SettingWithCopyError, f)

        # invalid warning as we are returning a new object
        # GH 8730
        df1 = DataFrame({'x': Series(['a', 'b', 'c']),
                         'y': Series(['d', 'e', 'f'])})
        df2 = df1[['x']]

        # this should not raise
        df2['y'] = ['g', 'h', 'i'] 
Example #7
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_frame_getitem_view(self):
        df = self.frame.T.copy()

        # this works because we are modifying the underlying array
        # really a no-no
        df['foo'].values[:] = 0
        assert (df['foo'].values == 0).all()

        # but not if it's mixed-type
        df['foo', 'four'] = 'foo'
        df = df.sort_index(level=0, axis=1)

        # this will work, but will raise/warn as its chained assignment
        def f():
            df['foo']['one'] = 2
            return df

        pytest.raises(com.SettingWithCopyError, f)

        try:
            df = f()
        except:
            pass
        assert (df['foo', 'one'] == 0).all() 
Example #8
Source File: test_multilevel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_xs_level(self):
        result = self.frame.xs('two', level='second')
        expected = self.frame[self.frame.index.get_level_values(1) == 'two']
        expected.index = expected.index.droplevel(1)

        tm.assert_frame_equal(result, expected)

        index = MultiIndex.from_tuples([('x', 'y', 'z'), ('a', 'b', 'c'), (
            'p', 'q', 'r')])
        df = DataFrame(np.random.randn(3, 5), index=index)
        result = df.xs('c', level=2)
        expected = df[1:2]
        expected.index = expected.index.droplevel(2)
        tm.assert_frame_equal(result, expected)

        # this is a copy in 0.14
        result = self.frame.xs('two', level='second')

        # setting this will give a SettingWithCopyError
        # as we are trying to write a view
        def f(x):
            x[:] = 10

        pytest.raises(com.SettingWithCopyError, f, result) 
Example #9
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_xs_level(self):
        result = self.frame.xs('two', level='second')
        expected = self.frame[self.frame.index.get_level_values(1) == 'two']
        expected.index = expected.index.droplevel(1)

        tm.assert_frame_equal(result, expected)

        index = MultiIndex.from_tuples([('x', 'y', 'z'), ('a', 'b', 'c'), (
            'p', 'q', 'r')])
        df = DataFrame(np.random.randn(3, 5), index=index)
        result = df.xs('c', level=2)
        expected = df[1:2]
        expected.index = expected.index.droplevel(2)
        tm.assert_frame_equal(result, expected)

        # this is a copy in 0.14
        result = self.frame.xs('two', level='second')

        # setting this will give a SettingWithCopyError
        # as we are trying to write a view
        def f(x):
            x[:] = 10

        pytest.raises(com.SettingWithCopyError, f, result) 
Example #10
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_frame_getitem_view(self):
        df = self.frame.T.copy()

        # this works because we are modifying the underlying array
        # really a no-no
        df['foo'].values[:] = 0
        assert (df['foo'].values == 0).all()

        # but not if it's mixed-type
        df['foo', 'four'] = 'foo'
        df = df.sort_index(level=0, axis=1)

        # this will work, but will raise/warn as its chained assignment
        def f():
            df['foo']['one'] = 2
            return df

        pytest.raises(com.SettingWithCopyError, f)

        try:
            df = f()
        except:
            pass
        assert (df['foo', 'one'] == 0).all() 
Example #11
Source File: test_chaining_and_caching.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_setting_with_copy_bug(self):

        # operating on a copy
        df = DataFrame({'a': list(range(4)),
                        'b': list('ab..'),
                        'c': ['a', 'b', np.nan, 'd']})
        mask = pd.isna(df.c)

        def f():
            df[['c']][mask] = df[['b']][mask]

        pytest.raises(com.SettingWithCopyError, f)

        # invalid warning as we are returning a new object
        # GH 8730
        df1 = DataFrame({'x': Series(['a', 'b', 'c']),
                         'y': Series(['d', 'e', 'f'])})
        df2 = df1[['x']]

        # this should not raise
        df2['y'] = ['g', 'h', 'i'] 
Example #12
Source File: test_chaining_and_caching.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_detect_chained_assignment():
    # Inplace ops, originally from:
    # http://stackoverflow.com/questions/20508968/series-fillna-in-a-multiindex-dataframe-does-not-fill-is-this-a-bug
    a = [12, 23]
    b = [123, None]
    c = [1234, 2345]
    d = [12345, 23456]
    tuples = [('eyes', 'left'), ('eyes', 'right'), ('ears', 'left'),
              ('ears', 'right')]
    events = {('eyes', 'left'): a,
              ('eyes', 'right'): b,
              ('ears', 'left'): c,
              ('ears', 'right'): d}
    multiind = MultiIndex.from_tuples(tuples, names=['part', 'side'])
    zed = DataFrame(events, index=['a', 'b'], columns=multiind)

    with pytest.raises(com.SettingWithCopyError):
        zed['eyes']['right'].fillna(value=555, inplace=True) 
Example #13
Source File: test_chaining_and_caching.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_setting_with_copy_bug(self):

        # operating on a copy
        df = DataFrame({'a': list(range(4)),
                        'b': list('ab..'),
                        'c': ['a', 'b', np.nan, 'd']})
        mask = pd.isna(df.c)

        def f():
            df[['c']][mask] = df[['b']][mask]

        pytest.raises(com.SettingWithCopyError, f)

        # invalid warning as we are returning a new object
        # GH 8730
        df1 = DataFrame({'x': Series(['a', 'b', 'c']),
                         'y': Series(['d', 'e', 'f'])})
        df2 = df1[['x']]

        # this should not raise
        df2['y'] = ['g', 'h', 'i'] 
Example #14
Source File: test_indexing.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_iloc_col(self):

        df = DataFrame(np.random.randn(4, 10), columns=lrange(0, 20, 2))

        result = df.iloc[:, 1]
        exp = df.loc[:, 2]
        assert_series_equal(result, exp)

        result = df.iloc[:, 2]
        exp = df.loc[:, 4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[:, slice(4, 8)]
        expected = df.loc[:, 8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # and that we are setting a copy
        def f():
            result[8] = 0.
        pytest.raises(com.SettingWithCopyError, f)
        assert (df[8] == 0).all()

        # list of integers
        result = df.iloc[:, [1, 2, 4, 6]]
        expected = df.reindex(columns=df.columns[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #15
Source File: test_indexing.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_iloc_row(self):
        df = DataFrame(np.random.randn(10, 4), index=lrange(0, 20, 2))

        result = df.iloc[1]
        exp = df.loc[2]
        assert_series_equal(result, exp)

        result = df.iloc[2]
        exp = df.loc[4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[slice(4, 8)]
        expected = df.loc[8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # setting it makes it raise/warn
        def f():
            result[2] = 0.
        pytest.raises(com.SettingWithCopyError, f)
        exp_col = df[2].copy()
        exp_col[4:8] = 0.
        assert_series_equal(df[2], exp_col)

        # list of integers
        result = df.iloc[[1, 2, 4, 6]]
        expected = df.reindex(df.index[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #16
Source File: test_indexing.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_iloc_col(self):

        df = DataFrame(np.random.randn(4, 10), columns=lrange(0, 20, 2))

        result = df.iloc[:, 1]
        exp = df.loc[:, 2]
        assert_series_equal(result, exp)

        result = df.iloc[:, 2]
        exp = df.loc[:, 4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[:, slice(4, 8)]
        expected = df.loc[:, 8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # and that we are setting a copy
        def f():
            result[8] = 0.
        pytest.raises(com.SettingWithCopyError, f)
        assert (df[8] == 0).all()

        # list of integers
        result = df.iloc[:, [1, 2, 4, 6]]
        expected = df.reindex(columns=df.columns[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #17
Source File: test_multilevel.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_xs_level_multiple(self):
        from pandas import read_table
        text = """                      A       B       C       D        E
one two three   four
a   b   10.0032 5    -0.5109 -2.3358 -0.4645  0.05076  0.3640
a   q   20      4     0.4473  1.4152  0.2834  1.00661  0.1744
x   q   30      3    -0.6662 -0.5243 -0.3580  0.89145  2.5838"""

        df = read_table(StringIO(text), sep=r'\s+', engine='python')

        result = df.xs(('a', 4), level=['one', 'four'])
        expected = df.xs('a').xs(4, level='four')
        tm.assert_frame_equal(result, expected)

        # this is a copy in 0.14
        result = df.xs(('a', 4), level=['one', 'four'])

        # setting this will give a SettingWithCopyError
        # as we are trying to write a view
        def f(x):
            x[:] = 10

        pytest.raises(com.SettingWithCopyError, f, result)

        # GH2107
        dates = lrange(20111201, 20111205)
        ids = 'abcde'
        idx = MultiIndex.from_tuples([x for x in cart_product(dates, ids)])
        idx.names = ['date', 'secid']
        df = DataFrame(np.random.randn(len(idx), 3), idx, ['X', 'Y', 'Z'])

        rs = df.xs(20111201, level='date')
        xp = df.loc[20111201, :]
        tm.assert_frame_equal(rs, xp) 
Example #18
Source File: test_multilevel.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_xs_level_multiple(self):
        from pandas import read_table
        text = """                      A       B       C       D        E
one two three   four
a   b   10.0032 5    -0.5109 -2.3358 -0.4645  0.05076  0.3640
a   q   20      4     0.4473  1.4152  0.2834  1.00661  0.1744
x   q   30      3    -0.6662 -0.5243 -0.3580  0.89145  2.5838"""

        df = read_table(StringIO(text), sep=r'\s+', engine='python')

        result = df.xs(('a', 4), level=['one', 'four'])
        expected = df.xs('a').xs(4, level='four')
        tm.assert_frame_equal(result, expected)

        # this is a copy in 0.14
        result = df.xs(('a', 4), level=['one', 'four'])

        # setting this will give a SettingWithCopyError
        # as we are trying to write a view
        def f(x):
            x[:] = 10

        pytest.raises(com.SettingWithCopyError, f, result)

        # GH2107
        dates = lrange(20111201, 20111205)
        ids = 'abcde'
        idx = MultiIndex.from_tuples([x for x in cart_product(dates, ids)])
        idx.names = ['date', 'secid']
        df = DataFrame(np.random.randn(len(idx), 3), idx, ['X', 'Y', 'Z'])

        rs = df.xs(20111201, level='date')
        xp = df.loc[20111201, :]
        tm.assert_frame_equal(rs, xp) 
Example #19
Source File: test_indexing.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_iloc_col(self):

        df = DataFrame(np.random.randn(4, 10), columns=lrange(0, 20, 2))

        result = df.iloc[:, 1]
        exp = df.loc[:, 2]
        assert_series_equal(result, exp)

        result = df.iloc[:, 2]
        exp = df.loc[:, 4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[:, slice(4, 8)]
        expected = df.loc[:, 8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # and that we are setting a copy
        with pytest.raises(com.SettingWithCopyError):
            result[8] = 0.

        assert (df[8] == 0).all()

        # list of integers
        result = df.iloc[:, [1, 2, 4, 6]]
        expected = df.reindex(columns=df.columns[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #20
Source File: test_indexing.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_iloc_row(self):
        df = DataFrame(np.random.randn(10, 4), index=lrange(0, 20, 2))

        result = df.iloc[1]
        exp = df.loc[2]
        assert_series_equal(result, exp)

        result = df.iloc[2]
        exp = df.loc[4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[slice(4, 8)]
        expected = df.loc[8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # setting it makes it raise/warn
        def f():
            result[2] = 0.
        pytest.raises(com.SettingWithCopyError, f)
        exp_col = df[2].copy()
        exp_col[4:8] = 0.
        assert_series_equal(df[2], exp_col)

        # list of integers
        result = df.iloc[[1, 2, 4, 6]]
        expected = df.reindex(df.index[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #21
Source File: test_indexing.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_iloc_row(self):
        df = DataFrame(np.random.randn(10, 4), index=lrange(0, 20, 2))

        result = df.iloc[1]
        exp = df.loc[2]
        assert_series_equal(result, exp)

        result = df.iloc[2]
        exp = df.loc[4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[slice(4, 8)]
        expected = df.loc[8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # setting it makes it raise/warn
        with pytest.raises(com.SettingWithCopyError):
            result[2] = 0.

        exp_col = df[2].copy()
        exp_col[4:8] = 0.
        assert_series_equal(df[2], exp_col)

        # list of integers
        result = df.iloc[[1, 2, 4, 6]]
        expected = df.reindex(df.index[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #22
Source File: test_indexing.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_fancy_getitem_slice_mixed(self):
        sliced = self.mixed_frame.iloc[:, -3:]
        assert sliced['D'].dtype == np.float64

        # get view with single block
        # setting it triggers setting with copy
        sliced = self.frame.iloc[:, -3:]

        with pytest.raises(com.SettingWithCopyError):
            sliced['C'] = 4.

        assert (self.frame['C'] == 4).all() 
Example #23
Source File: test_xs.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_xs_setting_with_copy_error(multiindex_dataframe_random_data):
    # this is a copy in 0.14
    df = multiindex_dataframe_random_data
    result = df.xs('two', level='second')

    # setting this will give a SettingWithCopyError
    # as we are trying to write a view
    msg = 'A value is trying to be set on a copy of a slice from a DataFrame'
    with pytest.raises(com.SettingWithCopyError, match=msg):
        result[:] = 10 
Example #24
Source File: test_xs.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_xs_setting_with_copy_error_multiple(four_level_index_dataframe):
    # this is a copy in 0.14
    df = four_level_index_dataframe
    result = df.xs(('a', 4), level=['one', 'four'])

    # setting this will give a SettingWithCopyError
    # as we are trying to write a view
    msg = 'A value is trying to be set on a copy of a slice from a DataFrame'
    with pytest.raises(com.SettingWithCopyError, match=msg):
        result[:] = 10 
Example #25
Source File: test_xs.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_xs_setting_with_copy_error(multiindex_dataframe_random_data):
    # this is a copy in 0.14
    df = multiindex_dataframe_random_data
    result = df.xs('two', level='second')

    # setting this will give a SettingWithCopyError
    # as we are trying to write a view
    msg = 'A value is trying to be set on a copy of a slice from a DataFrame'
    with pytest.raises(com.SettingWithCopyError, match=msg):
        result[:] = 10 
Example #26
Source File: test_xs.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_xs_setting_with_copy_error_multiple(four_level_index_dataframe):
    # this is a copy in 0.14
    df = four_level_index_dataframe
    result = df.xs(('a', 4), level=['one', 'four'])

    # setting this will give a SettingWithCopyError
    # as we are trying to write a view
    msg = 'A value is trying to be set on a copy of a slice from a DataFrame'
    with pytest.raises(com.SettingWithCopyError, match=msg):
        result[:] = 10 
Example #27
Source File: test_indexing.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_iloc_col(self):

        df = DataFrame(np.random.randn(4, 10), columns=lrange(0, 20, 2))

        result = df.iloc[:, 1]
        exp = df.loc[:, 2]
        assert_series_equal(result, exp)

        result = df.iloc[:, 2]
        exp = df.loc[:, 4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[:, slice(4, 8)]
        expected = df.loc[:, 8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # and that we are setting a copy
        def f():
            result[8] = 0.
        pytest.raises(com.SettingWithCopyError, f)
        assert (df[8] == 0).all()

        # list of integers
        result = df.iloc[:, [1, 2, 4, 6]]
        expected = df.reindex(columns=df.columns[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #28
Source File: test_indexing.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_iloc_row(self):
        df = DataFrame(np.random.randn(10, 4), index=lrange(0, 20, 2))

        result = df.iloc[1]
        exp = df.loc[2]
        assert_series_equal(result, exp)

        result = df.iloc[2]
        exp = df.loc[4]
        assert_series_equal(result, exp)

        # slice
        result = df.iloc[slice(4, 8)]
        expected = df.loc[8:14]
        assert_frame_equal(result, expected)

        # verify slice is view
        # setting it makes it raise/warn
        def f():
            result[2] = 0.
        pytest.raises(com.SettingWithCopyError, f)
        exp_col = df[2].copy()
        exp_col[4:8] = 0.
        assert_series_equal(df[2], exp_col)

        # list of integers
        result = df.iloc[[1, 2, 4, 6]]
        expected = df.reindex(df.index[[1, 2, 4, 6]])
        assert_frame_equal(result, expected) 
Example #29
Source File: test_setitem.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_frame_setitem_copy_raises(multiindex_dataframe_random_data):
    # will raise/warn as its chained assignment
    df = multiindex_dataframe_random_data.T
    msg = "A value is trying to be set on a copy of a slice from a DataFrame"
    with pytest.raises(com.SettingWithCopyError, match=msg):
        df['foo']['one'] = 2 
Example #30
Source File: test_multilevel.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_xs_level_multiple(self):
        from pandas import read_table
        text = """                      A       B       C       D        E
one two three   four
a   b   10.0032 5    -0.5109 -2.3358 -0.4645  0.05076  0.3640
a   q   20      4     0.4473  1.4152  0.2834  1.00661  0.1744
x   q   30      3    -0.6662 -0.5243 -0.3580  0.89145  2.5838"""

        df = read_table(StringIO(text), sep=r'\s+', engine='python')

        result = df.xs(('a', 4), level=['one', 'four'])
        expected = df.xs('a').xs(4, level='four')
        tm.assert_frame_equal(result, expected)

        # this is a copy in 0.14
        result = df.xs(('a', 4), level=['one', 'four'])

        # setting this will give a SettingWithCopyError
        # as we are trying to write a view
        def f(x):
            x[:] = 10

        pytest.raises(com.SettingWithCopyError, f, result)

        # GH2107
        dates = lrange(20111201, 20111205)
        ids = 'abcde'
        idx = MultiIndex.from_tuples([x for x in cart_product(dates, ids)])
        idx.names = ['date', 'secid']
        df = DataFrame(np.random.randn(len(idx), 3), idx, ['X', 'Y', 'Z'])

        rs = df.xs(20111201, level='date')
        xp = df.loc[20111201, :]
        tm.assert_frame_equal(rs, xp)