0

Purpose: write a function to delete all data frames that start with a specific prefix from the name space.

def del_dfs(prefix):
    df_list = [] # I know this list building stage may not be necessary but I need a list of included dfs for other purposes
    df_list.append([df for name, df in globals().items() if name.startswith(prefix) and isinstance(df, (pd.Series, pd.DataFrame))])
    for df in df_list:
        del df

Issue: This simply does not delete the dfs from the namespace. I guess it is related to the deletion happening inside the function but I can not figure out a solution.

update My overall purpose is to find all dfs in the namespace that start with a prefix, concatenate them (I already have a function for this), and then delete the individual dfs from the namespace to free up RAM as I now have them all in a concatenated df.

Saeed
  • 1,848
  • 1
  • 18
  • 26
  • You are carefully deleting a *list* from the *function's* namespace, in which it only exists because you assigned it there. You've been sent on a fool's errand, by the looks of it. – jonrsharpe Mar 20 '18 at 22:40
  • You are only deleting your reference to the df_list variable not the actual content. Instead of removing variables try to use more function namespaces and the garbage collector will take care of freeing memory for you. – larsl Mar 20 '18 at 22:44
  • @jonrsharpe does the function look any better now? Still does not work though. – Saeed Mar 20 '18 at 22:47
  • No, it still has the same problem - those identifiers are only assigned because *you just assigned them*. `del x` doesn't delete the object `x` previously referenced, it just decrements its reference count. It would be helpful to explain why this is a thing you're trying to do at all. – jonrsharpe Mar 20 '18 at 22:49
  • @jonrsharpe, just updated the question – Saeed Mar 20 '18 at 22:52
  • Could you go down another "why"? How and why are they all assigned to separate identifiers in the first place? If you'd used e.g. a dictionary, you could easily delete the keys from the dictionary. – jonrsharpe Mar 20 '18 at 22:54

1 Answers1

0

So, I figured this out using this post and this post:

def del_dfs(prefix):
    df_list = [] # I know this list building stage may not be necessary but I need a list of included dfs for other purposes
    df_list.append([df for name, df in globals().items() if name.startswith(prefix) and isinstance(df, (pd.Series, pd.DataFrame))])
    for name in list(globals()):
        if name.startswith(prefix):
            del globals()[name]
Saeed
  • 1,848
  • 1
  • 18
  • 26