0

Revised question: How to pass a pandas dataframe in to a function, then work with that dataframe without changing the original/global version.

Answer: Appears to be that it depends on the type of action you are performing and so in my opinion not possible without risking something changing. See this and this

I will post what I believe to be the correct handling for this situation below once I've confirmed it.

Original question:

This is something I expect has been asked before but for my research it seems I'm getting results that contradict convention.

Here is my code:

xTest = pd.DataFrame({'A' : ['Test']})
def MyFunction():
    _xTest=xTest
    _xTest['yPred'] = 1

    return _xTest

res=MyFunction()
xTest.head()

After it runs, xTest.head() shows that the 'yPred' column has been appended to it. From reading into Python scoping such as the URL below as well as somewhere else, I'm led to believe this should not be happening?

Python variable scope approach

Reddspark
  • 6,934
  • 9
  • 47
  • 64

2 Answers2

0

_xTest=xTest

This code means that xTest can also be referred using _xTest, so you're not only changing _xTest's value in function but also xTest's value.

Vivek
  • 1,465
  • 14
  • 29
  • Yep I realized that after I posted it. I tried this approach as I cannot seem to figure out how I can pass a parameter by value in Python, so I edited my code to get rid of passing parameters. The solution at the end of the URL I posted above is incorrect from what I can tell. Will delete this post if it turns out its me just not understanding variable scoping. – Reddspark May 25 '18 at 09:54
  • Further update: This seems to be the same problem as me: https://stackoverflow.com/questions/13419822/pandas-dataframe-copy-by-value I will be amending my question shortly once I've finished researching. – Reddspark May 25 '18 at 10:00
0

Ok from what I can tell the correct way to pass in a Pandas object if you want to complete avoid any risk of the global being modified is to do an explicit copy:

res=MyFunction(xTest.copy(deep=True))

The first link in the question I posted above is an excellent discourse on the issue of scoping and the second link provides the solution.

Reddspark
  • 6,934
  • 9
  • 47
  • 64