0

When I use pandas plot data in spyder. It will always show a warning message:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:494: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead. See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self.obj[item] = s

My scripts are below:

import pandas as pd

loc0 = r'D:\DC_BP00010.T0058_20190804_082758_15_14_PM2_1031.txt'
loc1 = r'D:\DC_BP00010.T0058_20190804_082758_17_16_PM1_1193.txt'

locs = [loc0,loc1]  # Update files in the list

parameters_must_be = ['Time', 'StepNo (Int)'] # Do not change this one
parameters = ["HeadBEPTrend1 (Float)"] # Key in parameters needed
parameters_all = parameters_must_be.append(parameters) 

endpoint_steps = [3] # Update endpoint steps in the list

Titles = 'BP00010' # Titles for plot

Colors = ['black','red','brown','crimson','olive','blue','yellow','darkorange','lime','purple','deeppink','dodgerblue','orange','indigo','darkslateblue','lawngreen','darkslategray','darkgreen','midnightblue','lightseagreen','gold','maroon','navy','teal']

dfs = []
sa = []
ss = []
axs = []
n = len(locs) # Number of datalog files

for i in range(0,n):
    dfs.append(pd.read_csv(locs[i], sep=('\t'), skiprows=6, usecols=parameters_all))
    ss.append(dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)])
    sa.append(ss[i].loc[:,'Time']-ss[i].loc[:,'Time'].iloc[0])
    ss[i].loc[:,'Time'] = sa[i].loc[:]

ax=ss[0].plot(x='Time', y=parameters, title=Titles, color=Colors[0:len(parameters)], linewidth=1)

if n>1:
    for i in range(1,n):
        axs.append(ss[i].plot(x='Time', y=parameters, color=Colors[len(parameters)*i:len(parameters)*i+len(parameters)], linewidth=1, ax=ax))

ax.set_xlim(0,80)
ax.set_ylim(0,60)
ax.set_ylabel('Endpoint Intensity')
ax.legend(['BP00010#14','BP00010#15','BP00010#16','BP00010#17'], bbox_to_anchor=(1.31, .5), loc=5, borderaxespad=0.)
3
  • on which line you are getting error? Commented Oct 17, 2019 at 10:12
  • Actually That is what I want to know Commented Oct 17, 2019 at 12:33
  • I guess, this line causes warning dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)]. Your syntax seems to be weird on this line. What you want to achieve on this line? Commented Oct 17, 2019 at 13:01

1 Answer 1

1

ss[i].loc[:,'Time'] = sa[i].loc[:] is causing the error:

  • You're trying to change a value in another dataframe.
  • ss is a list of dataframes
  • ss[i] is a specific dataframe that comes from dfs

ss and sa are lists of parts of dataframes created from the following:

  • ss.append(dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)])
    • ss comes from dfs
  • sa.append(ss[i].loc[:,'Time']-ss[i].loc[:,'Time'].iloc[0])
    • sa comes from ss
  • Creating a dataframe from other dataframes is fine, but doing so makes a reference to the original, not a copy.

Add .copy() when creating ss and sa:

  • ss.append(dfs[i][dfs[i]['StepNo (Int)'].isin(endpoint_steps)].copy())
  • sa.append(ss[i].loc[:,'Time']-ss[i].loc[:,'Time'].iloc[0].copy())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.