简体   繁体   English

Python3 类型错误:“值”必须是 str 或字节的实例,而不是元组

[英]Python3 TypeError: 'value' must be an instance of str or bytes, not a tuple

I am new to python and trying to investigate the stock market with it.我是 python 的新手,并试图用它来调查股票市场。 I've got a double indexed dataframe of stock data from a few companies and I'm trying to plot it on a graph where each line is a company.我有一个来自几家公司的股票数据的双索引数据框,我试图将它绘制在每条线是一家公司的图表上。 I was getting the error in the title on the plt.plot() line, and when I tried to just select stock from one company and I got the same error我在plt.plot()行的标题中遇到错误,当我尝试只从一家公司中选择股票时,我遇到了同样的错误

# import libraries
import pandas_datareader as pdr
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

# load the data
def get(tickers, startdate, enddate):
    def data(ticker):
        return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
    datas = map (data, tickers)
    return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date']))

tickers = ['AAPL', 'MSFT', 'IBM', 'GOOG']
all_data = get(tickers, datetime.datetime(2018, 1, 1), datetime.datetime(2020, 10, 1))

# AAPL only
aapl = all_data[all_data.index.get_loc('AAPL')]

# visualize the stock price
plt.figure(figsize=(12.2, 4.5))
plt.plot(aapl['Close'], label='Close')
plt.title('Close Price History')
plt.xlabel('Date')
plt.ylabel('Price USD ($)')
plt.show()

EDIT: here is the full traceback as requested in comments编辑:这是评论中要求的完整追溯

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-506d7434af31> in <module>
      1 # visualize the stock price
      2 plt.figure(figsize=(12.2, 4.5))
----> 3 plt.plot(all_data['Close'], label='Close')
      4 plt.title('Close Price History')
      5 plt.xlabel('Date')

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   2822 @_copy_docstring_and_deprecators(Axes.plot)
   2823 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2824     return gca().plot(
   2825         *args, scalex=scalex, scaley=scaley,
   2826         **({"data": data} if data is not None else {}), **kwargs)

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1741         """
   1742         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1744         for line in lines:
   1745             self.add_line(line)

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/axes/_base.py in __call__(self, data, *args, **kwargs)
    271                 this += args[0],
    272                 args = args[1:]
--> 273             yield from self._plot_args(this, kwargs)
    274 
    275     def get_next_color(self):

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    392 
    393         if self.axes.xaxis is not None:
--> 394             self.axes.xaxis.update_units(x)
    395         if self.axes.yaxis is not None:
    396             self.axes.yaxis.update_units(y)

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/axis.py in update_units(self, data)
   1461         neednew = self.converter != converter
   1462         self.converter = converter
-> 1463         default = self.converter.default_units(data, self)
   1464         if default is not None and self.units is None:
   1465             self.set_units(default)

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/category.py in default_units(data, axis)
    105         # the conversion call stack is default_units -> axis_info -> convert
    106         if axis.units is None:
--> 107             axis.set_units(UnitData(data))
    108         else:
    109             axis.units.update(data)

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/category.py in __init__(self, data)
    174         self._counter = itertools.count()
    175         if data is not None:
--> 176             self.update(data)
    177 
    178     @staticmethod

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/category.py in update(self, data)
    209         for val in OrderedDict.fromkeys(data):
    210             # OrderedDict just iterates over unique values in data.
--> 211             cbook._check_isinstance((str, bytes), value=val)
    212             if convertible:
    213                 # this will only be called so long as convertible is True.

/opt/miniconda3/envs/ds383/lib/python3.8/site-packages/matplotlib/cbook/__init__.py in _check_isinstance(_types, **kwargs)
   2233     for k, v in kwargs.items():
   2234         if not isinstance(v, types):
-> 2235             raise TypeError(
   2236                 "{!r} must be an instance of {}, not a {}".format(
   2237                     k,

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

As mentioned in the comments it is important to first understand the traceback:正如评论中提到的,首先了解回溯很重要:

  File "type_error.py", line 25, in <module>
    plt.plot(aapl['Close'], label='Close')
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2842, in plot
    **({"data": data} if data is not None else {}), **kwargs)
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1743, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 273, in __call__
    yield from self._plot_args(this, kwargs)
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 394, in _plot_args
    self.axes.xaxis.update_units(x)
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/axis.py", line 1463, in update_units
    default = self.converter.default_units(data, self)
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/category.py", line 107, in default_units
    axis.set_units(UnitData(data))
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/category.py", line 176, in __init__
    self.update(data)
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/category.py", line 211, in update
    cbook._check_isinstance((str, bytes), value=val)
  File "/home/phihei00/.virtualenvs/stackoverlow/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 2251, in _check_isinstance
    type_name(type(v))))
TypeError: 'value' must be an instance of str or bytes, not a tuple

It seems you are passing something to plt.plot() as first argument that is wrong.似乎您将某些内容传递给plt.plot()作为第一个错误的参数。

Let's use a debugger ( breakpoint() if python >= 3.7 else import ipdb; ipdb.set_trace() or your favorite editor) to inspect this:让我们使用调试器( breakpoint() if python >= 3.7 else import ipdb; ipdb.set_trace()或你最喜欢的编辑器)来检查这个:

ipdb> type(aapl["Close"])
<class 'pandas.core.series.Series'>

This will show you the type of your data.这将显示您的数据类型。 This combined with a short google search will lead you to the pandas documentationhttps://pandas.pydata.org/docs/reference/api/pandas.Series.html where you will find that this class has a method to_numpy这与简短的谷歌搜索相结合将带您到熊猫文档https://pandas.pydata.org/docs/reference/api/pandas.Series.html在那里您会发现这个类有一个方法to_numpy

So we insert the patch:所以我们插入补丁:

plt.plot(aapl['Close'].to_numpy(), label='Close')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: 'value' 必须是 str 或 bytes 的实例,而不是浮点数 - TypeError: 'value' must be an instance of str or bytes, not a float ElementTree TypeError“write()参数必须是str,而不是Python3中的字节” - ElementTree TypeError “write() argument must be str, not bytes” in Python3 在 Python3 中解码字节字符串时出错 [TypeError: must be str, not bytes] - Error decoding byte string in Python3 [TypeError: must be str, not bytes] Python3 configparser从第一个arg开始必须是字节或字节元组,而不是str - Python3 configparser startswith first arg must be bytes or a tuple of bytes, not str TypeError:initial_value必须是str还是none,而不是python 3中的字节? - TypeError: initial_value must be str or none, not bytes in python 3? Tkinter类“必须不是元组” TypeError Python - Tkinter Class “must be str not tuple” TypeError Python Python MySQL TypeError:必须是str,而不是元组 - Python MySQL TypeError: must be str, not tuple 使用Magpie + Tensorflow / Python3,“ TypeError:join()参数必须为str或字节,而不是&#39;NoneType&#39;” - “TypeError: join() argument must be str or bytes, not 'NoneType'” using Magpie+Tensorflow/Python3 Python - 类型错误:write() 参数必须是 str,而不是字节 - Python - TypeError: write() argument must be str, not bytes Python sendto错误TypeError:必须为str,而不是字节 - Python sendto Error TypeError: must be str, not bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM