# 8.30:  Trigger a "Copy of a Slice" Warning.

# Here we are creating a slice by using a double subscript:

import pandas as pd

# set the "Copy-on-Write" functionality to avoid
# "copy of a slice" warning and behavior
# pd.options.mode.copy_on_write = True


dfi = pd.DataFrame({'c1': [ 1,  2, 3,  4],
                    'c2': [16, 17, 18, 19],
                    'c3': [21, 22, 23, 24],
                    'c4': [26, 27, 28, 29] },
           index = ['r1', 'r2', 'r3', 'r4'])


dfis = dfi[ dfi.c1 > 2 ]
print(dfis)

                  #     c1  c2  c3  c4
                  # r4   3  18  23  28
                  # r5   4  19  24  29


dfis.c3 = dfis.c1 * dfis.c2


# alternative: using .loc[] indexing
# dfis.loc[ :, 'c3' ] = dfis.c1 * dfis.c2

# This setting forces pandas to create a new copy of the DataFrame any time we attempt to write to a
# slice (which usually triggers the tricky and confusing "copy of a slice" warning).

