Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I am getting the following error:

TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid 
Traceback (most recent call last):
  File "/var/task/etd.py", line 77, in lambda_handler
    data = pd.concat(concated_data, ignore_index=True)

In the following block:

for idx, day_row in curr_df.iterrows():
    av_last_month, rate_end_last_mon = last_months_avs(df_to_date, day_row['From_Currency'], day_row['To_Currency'], day_row['Obs_Date'])

concated_data = ({'Obs_Date': day_row['Obs_Date'], 'From_Currency': day_row['From_Currency'], 
'To_Currency': day_row['To_Currency'], 'Rate': day_row['Rate'],
'PE_Rate': av_last_month, 'Mon_End_Rate': rate_end_last_mon})

data = pd.concat(concated_data, ignore_index=True)

new_df = pd.DataFrame(data)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One of your values is a string and not a Series or a DataFrame:

concated_data = ({'key1': pd.Series(), 'key2': pd.Series()})
pd.concat(concated_data, ignore_index=True)

# Output
Series([], dtype: object)

However:

concated_data = ({'key1': pd.Series(), 'key2': pd.Series(), 'key3': ''})
pd.concat(concated_data, ignore_index=True)

# Output
...
TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

To debug, try:

for key, val in concated_data.items():
    print(key, type(val))

# Output
key1 <class 'pandas.core.series.Series'>
key2 <class 'pandas.core.series.Series'>
key3 <class 'str'>

Note: I think you should also use axis=1 as argument of pd.concat.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading