Home > Mobile >  pandas.Series.replace not working with yfinance
pandas.Series.replace not working with yfinance

Time:03-29

I'm having issues to remove the dollar symbol from a Series after importing data from Yahoo Finance (yfinance). Does anyone know what I am doing wrong? Here is my code.

import yfinance as yf

start_date = '2016-01-01'

yf_eth_df = yf.download('ETH-USD',start_date, today)
yf_eth_df = yf_eth_df.reset_index()


yf_eth_df.tail()

I've created a custom column to get the variation in percentage between the Open value and the Highest value.

The output is:

    Date    Open    High    Low Close   Adj Close   Volume  variation_open_high
0   2017-11-09  $308.64 $329.45 $307.06 $320.88 $320.88 893249984   $6.32
1   2017-11-10  $320.67 $324.72 $294.54 $299.25 $299.25 885985984   $1.25
2   2017-11-11  $298.59 $319.45 $298.19 $314.68 $314.68 842300992   $6.53
3   2017-11-12  $314.69 $319.15 $298.51 $307.91 $307.91 1613479936  $1.40
4   2017-11-13  $307.02 $328.42 $307.02 $316.72 $316.72 1041889984  $6.51

For some reason, the data frame is being created with the $ symbol. I want to remove it from my last column using yf_eth_df['variation_open_high'] = yf_eth_df['variation_open_high'].replace("$", '') but it is not working.

CodePudding user response:

Series.replace only replaces values that fully match. To replace substrings use Series.str.replace

yf_eth_df['variation_open_high'] = yf_eth_df['variation_open_high'].str.replace("$", '', regex=False)

Note that after removing the dollar sign the values are still strings. It seems that you want them as floats, so you have to explicitly convert them

yf_eth_df['variation_open_high'] = ( 
    yf_eth_df['variation_open_high'].str.replace("$", '', regex=False)
                                    .astype(float)
)
  • Related