TypeError: 'value' in Matplotlib, an instance of str or bytes, not a float

my code:

stirs = pd.read_csv(r'C:/Users/Owner/Documents/Research/SOMA/FRED_reserves/SOMArates2.csv', \ 
na_values=" ", usecols=[0,1,2,3,4,5], parse_dates=['DATE'] ).fillna(value=0)
 stirs.info()  
stirs.columns = stirs.columns.str.strip()  
stirs['DATE'] = pd.to_datetime(stirs['DATE'],format='%m/%d/%Y')  
date = stirs.loc[:, "DATE"]  
eff  = stirs.loc[:, "EFFR"]  
repo = stirs.loc[:, "RPONTSYD"]  
rrepo = stirs.loc[:, "RRPONTSYD"]  
mrepo = stirs.loc[:, "RPTSYD"]  
mrrepo = stirs.loc[:, "RRPTSYD"]  
print(eff)  
print(mrrepo)  
x=stirs['DATE']
plt.figure(figsize=(5, 2.7), layout='constrained')
plt.plot(x, eff, label='Effective Fed Funds')  [**]
plt.plot(x, repo, label='Repos')
plt.plot(x, rrepo, label='Reverse repo')  
plt.plot(x, mrepo, label='Repos_OMO')  
plt.plot(x, mrrepo, label='Reverse Repos_OMO')  
plt.xlabel('Date')  
plt.ylabel('y label')  
plt.title("Effective FF rate, repo, reverse repo")  
plt.legend()  

Can you indicate what fails and the full error message? And maybe make a failing self contained example? We have no idea what is in your file and if you try and reproduce the rrror you can usually tell what caused it.

Hours and hours trying to find this error. I’m now just guessing. I have indicated the full error. I’ve looke at the data.
Python prints out the values correctly

I did try to plot only the one variable rather than the full plot:

There are 6 columns of data: DATE EFFR RPONTSYD RRPONTSYD RPTSYD RRPTSYD
Matplotlib has no problem plotting the last 4 variable: RPONTSYD RRPONTSYD RPTSYD RRPTSYD
But the line for plotting the first variable EFFR triggers the error:
TypeError: ‘value’ must be an instance of str or bytes, not a float

EFFR is the first variable after DATE, so I’m thinking that the problem is the date format: the date in csv file is m/d/Y but python records it as Y/m/d
effrate = stirs.loc[:,“EFFR”]

I’ve deleted last row of the excel sheet, I’ve looked at the EFFR column for anomalies

I’ve tried plotting EFFR alone: and get the same error
plt.figure(figsize=(5, 2.7), layout=‘constrained’)
plt.plot(x, effrate, label=‘Fed Funds’)

When I plot all five variables, I get the same error for EFFR
fig, ax1 = plt.subplots()
color1 = ‘tab:red’
color = ‘tab:blue’
color2 = ‘tab:green’
ax1.set_xlabel(‘Date’)
ax1.set_ylabel(‘Percent (x.xx)’, color=color1)
ax1.tick_params(axis=‘y’, labelcolor=color)
lns1 = ax1.plot(stirs[‘DATE’], effrate, label=‘EFFR’,linestyle =’–’,linewidth=.5, color=‘c’)
lns2 = ax1.plot(stirs[‘DATE’], repo, label=‘Repos’,linestyle =’–’,linewidth=.5, color=‘b’)
lns3 = ax1.plot(x, mrepo, label=‘Repos_OMO’,linestyle =’-.’, linewidth=.75, color=‘g’)
ax2 = ax1.twinx()
ax2.set_ylabel(‘Level’, color=color)
ax2.tick_params(axis=‘y’, labelcolor=color)

instantiate a second axes that shares the same x-axis

lns4 = ax2.plot(x, rrepo, label=‘Reverse repo’,linestyle =’–’, linewidth=.3,color=‘k’)
lns5 = ax2.plot(x, mrrepo, label=‘Reverse Repos_OMO’,linestyle =’-.’, linewidth=.2,color=‘m’)
lns = lns1+lns2+lns3+lns4+lns5
labs = [l.get_label() for l in lns]
plt.legend(lns, labs, loc=0)
plt.title(“Effective FF rate, repo, reverse repo”)

plt.legend()

If I plot only the last 4 variables, the plot is fine

···

Nancy Hammond, Ph.D
nahammond@gmail.com
312.989.3854

The csv file, first few line:

···

Nancy Hammond, Ph.D
nahammond@gmail.com
312.989.3854

Ok, it really is the case that the TypeError: ‘value’ must be an instance of str or bytes, not a float applies to the variable EFFR.

I retyped the whole series for EFFR and still got the above error.

I need to learn how python and matplotlib treat variable types.

This is not happening to any other variable in the series.
And I’ve ruled out a problem with a date.

Time for a walk outside.

···

Nancy Hammond, Ph.D
nahammond@gmail.com
312.989.3854

ValueError: could not convert string to float: ‘.’

stirs = pd.read_csv(r’C:/Users/Owner/Documents/Research/SOMA/FRED_reserves/SOMArates4.csv’,
na_values=" ", usecols=[0,1,2,3,4,5],dtype={‘RPONTSYD’: float, ‘RRPONTSYD’: float, ‘RPTSYD’: float, ‘EFFR’: float}, parse_dates=[‘DATE’]).fillna(value=0)

···

Nancy Hammond, Ph.D
nahammond@gmail.com
312.989.3854

Last message 5/26/2022 10:36 pm

The problem converting with dtype to float is the symbol ‘.’ for missing value
Although I have converted them to NaNs

https://stackoverflow.com/questions/54065751/valueerror-could-not-convert-string-to-float-when-converting-input

stirs = pd.read_csv(r’C:/Users/Owner/Documents/Research/SOMA/FRED_reserves/SOMArates4.csv’,
na_values=" ", usecols=[0,1,2,3,4,5],dtype={‘RPONTSYD’: float, ‘RRPONTSYD’: float, ‘RPTSYD’: float, ‘EFFR’: float}, parse_dates=[‘DATE’]).fillna(value=0)

dt = np.dtype(np.int32)

DATE EFFR RPONTSYD RRPONTSYD RPTSYD RRPTSYD

stirs.replace(’.’, np.nan, inplace=True)
stirs = stirs.replace(r’^\s*$’, np.nan, regex=True)
stirs.info()
stirs.columns = stirs.columns.str.strip()

stirs[‘DATE’] = pd.to_datetime(stirs[‘DATE’])

stirs[‘DATE’] = pd.to_datetime(stirs[‘DATE’], format="%Y%m%d")
date = stirs.loc[0:, “DATE”]
effrate = stirs.loc[:,“EFFR”]

eff = float(effrate)

TypeError: cannot convert the series to <class ‘float’

I’ll work on this tomorrow 5/27/2022

···

Nancy Hammond, Ph.D
nahammond@gmail.com
312.989.3854