Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.6K
Visualizações
TypeError: cannot convert the series to <class 'float'>

I have a dataframe (df) that looks like:

date                 A
2001-01-02      1.0022
2001-01-03      1.1033
2001-01-04      1.1496
2001-01-05      1.1033

2015-03-30    126.3700
2015-03-31    124.4300
2015-04-01    124.2500
2015-04-02    124.8900

For the entire time-series I'm trying to divide today's value by yesterdays and log the result using the following:

df["B"] = math.log(df["A"] / df["A"].shift(1))

However I get the following error:

TypeError: cannot convert the series to <class 'float'>

How can I fix this? I've tried to cast as float using:

df["B"] .astype(float)

But can't get anything to work.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use numpy.log instead. Math.log is expecting a single number, not array.

over 4 years ago · Santiago Trujillo Relatório

0

You can use lambda operator to apply your functions to the pandas data frame or to the series. More specifically if you want to convert each element on a column to a floating point number, you should do it like this:

df['A'].apply(lambda x: float(x))

here the lambda operator will take the values on that column (as x) and return them back as a float value.

over 4 years ago · Santiago Trujillo Relatório

0

If you just write df["A"].astype(float) you will not change df. You would need to assign the output of the astype method call to something else, including to the existing series using df['A'] = df['A'].astype(float). Also you might want to either use numpy as @user3582076 suggests, or use .apply on the Series that results from dividing today's value by yesterday's.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda