ste
ste

Reputation: 1

Pandas TypeError: unsupported operand type(s) for -: 'float' and 'str' (Python)

Combining Series into DataFrames Using the aud_usd_lst and eur_aud_lst lists defined in the scaffold on the right, perform the following tasks:

  1. Create a series named aud_usd_series with non-missing quotes for the AUD/USD exchange rate. Specifically:

The series should have dates as row labels. There should be no missing AUD/USD values.

  1. Create a series named eur_aud_series with non-missing quotes for the EUR/AUD exchange rate. Specifically:

The series should have dates as row labels. There should be no missing EUR/AUD values.

  1. Combine the two series into a data frame named df, so it has the dates as row labels and 'AUD/USD', 'EUR/AUD' as column labels.
import pandas as pd
import numpy as np
from unanswered import *

aud_usd_lst = [
    ('2020-09-08', 0.7280),
    ('2020-09-09', 0.7209),
    ('2020-09-11', 0.7263),
    ('2020-09-14', 0.7281),
    ('2020-09-15', 0.7285),
    ]

eur_aud_lst = [
    ('2020-09-08',  1.6232),
    ('2020-09-09',  1.6321),
    ('2020-09-10',  1.6221),
    ('2020-09-11',  1.6282),
    ('2020-09-15',  1.6288),
    ]

Here is my Code:

aud_usd_series = pd.Series(np.array(aud_usd_lst)[:,1], index=np.array(aud_usd_lst)[:,0])
aud_usd_series

eur_aud_series = eur_aud_series = pd.Series(np.array(eur_aud_lst)[:,1], index=np.array(eur_aud_lst)[:,0])
eur_aud_series

df = pd.DataFrame([aud_usd_series,eur_aud_series]).T
df.columns = ['AUD/USD','EUR/AUD']
df

I tried to run the code and it says

TypeError: unsupported operand type(s) for -: 'float' and 'str'

ANY Suggestion?

Upvotes: 0

Views: 321

Answers (3)

Hanley Soilsmith
Hanley Soilsmith

Reputation: 629

Put all of your code in the edit feature of codex, and the error in the instruction box. Tell it to fix the error.

Upvotes: 0

Josh
Josh

Reputation: 76

If this is the output you want based on the dataset in your question:

         date  aud/usd  eur/aud
0  2020-09-08   0.7280   1.6232
1  2020-09-09   0.7209   1.6321
2  2020-09-11   0.7263   1.6282
3  2020-09-15   0.7285   1.6288

Then, this is the code:

from io import StringIO
import pandas as pd

aud_usd_lst = [
    ('2020-09-08', 0.7280),
    ('2020-09-09', 0.7209),
    ('2020-09-11', 0.7263),
    ('2020-09-14', 0.7281),
    ('2020-09-15', 0.7285),
    ]
eur_aud_lst = [
    ('2020-09-08',  1.6232),
    ('2020-09-09',  1.6321),
    ('2020-09-10',  1.6221),
    ('2020-09-11',  1.6282),
    ('2020-09-15',  1.6288),
    ]

temp1 = []
temp2 = []
date_list = []
for x in aud_usd_lst:
    temp1.append(x[0])
for x in eur_aud_lst:
    temp2.append(x[0])
date_list = sorted(list(set(temp1).intersection(temp2)))

new_df_as_string = "date,aud/usd,eur/aud\n"
for x in date_list:
    new_df_as_string = new_df_as_string +x+","
    for y in aud_usd_lst:
        if y[0] == x:
            new_df_as_string = new_df_as_string +str(y[1])+","
    for z in eur_aud_lst:
        if z[0] == x:
            new_df_as_string = new_df_as_string +str(z[1])+"\n"

new_df_as_file = StringIO(new_df_as_string)
df = pd.read_csv(new_df_as_file, sep=",")
print(df)

Upvotes: 1

Shyamaray Aravattu
Shyamaray Aravattu

Reputation: 1

TypeError: unsupported operand type(s) for -: 'float' and 'str'

This means you make operation between float and string that not possible 🙂

Upvotes: 0

Related Questions