当前位置: 首页>>代码示例>>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;未经允许,请勿转载。