當前位置: 首頁>>代碼示例>>Python>>正文


Python common.SettingWithCopyError方法代碼示例

本文整理匯總了Python中pandas.core.common.SettingWithCopyError方法的典型用法代碼示例。如果您正苦於以下問題:Python common.SettingWithCopyError方法的具體用法?Python common.SettingWithCopyError怎麽用?Python common.SettingWithCopyError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas.core.common的用法示例。


在下文中一共展示了common.SettingWithCopyError方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_setting_with_copy_bug

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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'] 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:23,代碼來源:test_chaining_and_caching.py

示例2: test_detect_chained_assignment

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_chaining_and_caching.py

示例3: test_xs_level

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:26,代碼來源:test_multilevel.py

示例4: test_frame_getitem_view

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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() 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:26,代碼來源:test_multilevel.py

示例5: test_setting_with_copy_bug

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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'] 
開發者ID:securityclippy,項目名稱:elasticintel,代碼行數:23,代碼來源:test_chaining_and_caching.py

示例6: test_xs_setting_with_copy_error

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:12,代碼來源:test_xs.py

示例7: test_xs_setting_with_copy_error_multiple

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:12,代碼來源:test_xs.py

示例8: test_frame_setitem_copy_raises

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:8,代碼來源:test_setitem.py

示例9: test_fancy_getitem_slice_mixed

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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() 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:14,代碼來源:test_indexing.py

示例10: test_iloc_row

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:31,代碼來源:test_indexing.py

示例11: test_iloc_col

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:30,代碼來源:test_indexing.py

示例12: test_xs_level_multiple

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:36,代碼來源:test_multilevel.py

示例13: test_iloc_row

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:31,代碼來源:test_indexing.py

示例14: test_iloc_col

# 需要導入模塊: from pandas.core import common [as 別名]
# 或者: from pandas.core.common import SettingWithCopyError [as 別名]
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) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:30,代碼來源:test_indexing.py


注:本文中的pandas.core.common.SettingWithCopyError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。