Adding a dictionary to a row in a Pandas DataFrame using concat in pandas 1.4

Viewed 720

After updating to pandas 1.4, I now receive the following warning when using frame.append to append a dictionary to a Pandas DataFrame.

FutureWarning: The frame.append method is deprecated and will be
removed from pandas in a future version. Use pandas.concat instead.

Below is the code. This still works, though I would like to resolve the warning.

report = report.append({
                "period":period,
                "symbol":symbol,
                "start_date":start_date,
                "start_price":start_price,
                "start_market_cap":start_market_cap,
                "end_date":end_date,
                "end_price":end_price,
                "end_market_cap":end_market_cap,
                "return":return_
            },ignore_index=True)

I have updated the code to the below, which kicks a different warning:

report = pd.concat([report,{
                "period":period,
                "symbol":symbol,
                "start_date":start_date,
                "start_price":start_price,
                "start_market_cap":start_market_cap,
                "end_date":end_date,
                "end_price":end_price,
                "end_market_cap":end_market_cap,
                "return":return_
            }],ignore_index=True)

TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid

2 questions: Is the first warning wrong? What is the pandas 1.4 way to achieve this? Thanks.

2 Answers

Use loc to assign a single row value:

report.loc[len(report)] = {"period":period,
                           "symbol":symbol,
                           "start_date":start_date,
                           "start_price":start_price,
                           "start_market_cap":start_market_cap,
                           "end_date":end_date,
                           "end_price":end_price,
                           "end_market_cap":end_market_cap,
                           "return":return_
                          }
report = pd.concat([report, pd.DataFrame([mydict])], ignore_index=True)

where mydict is the row you want added.

Unlike append, concat does not support dicts directly.

append was deprecated in pandas 1.4.0

Related