TypeError:' value'在matplotlib中,str或字节的实例,而不是浮点

发布于 2025-01-31 02:40:57 字数 2719 浏览 1 评论 0原文

我无法弄清楚为什么在代码的第一行中遇到一个值类型错误。 我查看了TypeError问题的其他答案,但看不到错误。

我的模块

from datetime import datetime  
import matplotlib.pyplot as plt  
import matplotlib.animation as animation   
import matplotlib as mpl   
import matplotlib.font_manager as font_manager  
mpl.rcParams['font.family'] = 'sans-serif'  
mpl.rcParams['font.sans-serif'] = 'Lato'  
import numpy as np  
import pandas as pd

我的数据框看起来像这样 | | |计数|非无效| dtype | | --- | ------------ | ------- | ------------------------------------------------- - | | 0 |日期| 1305 |非无效| datetime64 [ns] | | 1 | effr | 1305 |非无效|对象| | 2 | Prontsyd | 1306 |非无效|对象| | 3 | rrpontsyd | 1306 |非无效|对象| | 4 | rptsyd | 1306 |非无效|对象| | 5 | rrptsyd | 1306 |非无效|对象|

该错误发生在以下代码的第一行上,

stirs = pd.read_csv(r'C:/Users/Owner/Documents/Research/SOMA/FRED_reserves/SOMArates2.csv',\
                   verbose =True , warn_bad_lines = True, \
                   usecols=[0,1,2,3,4,5], parse_dates=['DATE'] )
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()  
plt.savefig('C:/Users/Owner/Documents/Research/SOMA/Soma_stirs.eps')
计数非零dtype
0日1305non-nulldateTime64 [ns]
1effr1305非零对象
2prontsyd1306非零对象
3rrpontsyd1306非零对象
4rptsyd1306rptsyd 1306 rptsyd 1306非null对象
5rrptsyd1306non-nullobjectplt.show()

我从上面的代码中获得此错误:

TypeError: 'value' must be an instance of str or bytes, not a float

I cannot figure out why I'm getting a value type error in the first line of my code.
I've looked at the other answers to TypeError problems, but cannot see the error.

My Modules

from datetime import datetime  
import matplotlib.pyplot as plt  
import matplotlib.animation as animation   
import matplotlib as mpl   
import matplotlib.font_manager as font_manager  
mpl.rcParams['font.family'] = 'sans-serif'  
mpl.rcParams['font.sans-serif'] = 'Lato'  
import numpy as np  
import pandas as pd

My Dataframe looks like this
| | | Count | Non-Null | Dtype |
|---|-----------|-------|----------|----------------|
| 0 | DATE | 1305 | non-null | datetime64[ns] |
| 1 | EFFR | 1305 | non-null | object |
| 2 | PRONTSYD | 1306 | non-null | object |
| 3 | RRPONTSYD | 1306 | non-null | object |
| 4 | RPTSYD | 1306 | non-null | object |
| 5 | RRPTSYD | 1306 | non-null | object |

The error occurs on the first line of the code below

stirs = pd.read_csv(r'C:/Users/Owner/Documents/Research/SOMA/FRED_reserves/SOMArates2.csv',\
                   verbose =True , warn_bad_lines = True, \
                   usecols=[0,1,2,3,4,5], parse_dates=['DATE'] )
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()  
plt.savefig('C:/Users/Owner/Documents/Research/SOMA/Soma_stirs.eps')
CountNon-NullDtype
0DATE1305non-nulldatetime64[ns]
1EFFR1305non-nullobject
2PRONTSYD1306non-nullobject
3RRPONTSYD1306non-nullobject
4RPTSYD1306non-nullobject
5RRPTSYD1306non-nullobjectplt.show()

I get this error from the code above:

TypeError: 'value' must be an instance of str or bytes, not a float

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文