0

Python-beginner here. I'm trying to fix a deprecation warning in:

df = gpd.GeoDataFrame(columns=['location', 'geometry'])
for dir, subdir, files in os.walk(StartDir):
    for fname in files:
        if fname.endswith(".tif"):
            df = df.append({'location': fname, 'geometry': getBounds(os.path.join(dir+"/", fname))}, ignore_index=True)

by replacing the append line with:

df = gpd.pd.concat(df,{'location': fname, 'geometry': getBounds(os.path.join(dir+"/", fname))}, ignore_index=True)

which leads to this error message:

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "GeoDataFrame"

What am I missing?

1 Answers1

1

In order for concat to work, it needs a list of dataframes. You can make a temporary dataframe in each iteration of your loop using the dictionary notation {} you already have, but by passing it to the pd.DataFrame constructor, like so:

df = gpd.GeoDataFrame(columns=['location', 'geometry'])
for dir, subdir, files in os.walk(StartDir):
    for fname in files:
        if fname.endswith(".tif"):
        # create a temporary df with the desired values, it necessary to specify the index
        df_to_append = gpd.pd.DataFrame({'location': fname, 'geometry': getBounds(os.path.join(dir+"/", fname))}, index=[0])

        # here the temporary dataframe is appended to the original dataframe each iteration of the loop
        # by passing a list [] of the orignal dataframe and the temporary one to `pd.concat()`  
        # importantly, the index is now ignored to renumber each row sequentially
        df = gpd.pd.concat([df, df_to_append], ignore_index=True)

Matt
  • 16,843
  • 3
  • 21
  • 52
  • That works, thanks. I think, I understand it also. The main point seems to be that Python or the library does not understand that the object is a DataFrame without creating an object of the type DataFrame and assigning the data to it. Is that a Python thing or specific to the library? – Stefan Gofferje Oct 15 '22 at 19:33