【问题标题】:Unpacking numpy using ndarray使用 ndarray 解压 numpy
【发布时间】:2018-10-31 16:47:57
【问题描述】:

我是 python 新手。任何帮助将不胜感激。

我想show this graph ,使用我尝试过的第一块代码,但是当我尝试运行这段代码时:

date, value = np.loadtxt(revenue_ar, delimiter=',', unpack=True, converters={ 0: bytespdate2num('%Y-%m-%d')})

使用revenue_ar (numpy.ndarray) 会弹出此错误消息:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

第一段代码:

import time
import requests
import intrinio
import pandas as pd
import numpy as np    

api_username = 'hidden'
api_password = 'hidden'

def bytespdate2num(fmt, encoding='utf-8'):
    strconverter = mdates.strpdate2num(fmt)
    def bytesconverter(b):
        s = b.decode(encoding)
        return strconverter(s)
    return bytesconverter

ticker = 'AAPL' 
revenue_data = requests.get('https://api.intrinio.com/historical_data?identifier=' + ticker + '&item=totalrevenue', auth=(api_username, api_password))
revenue1 = revenue_data.json()['data'] 
revenue = pd.DataFrame(revenue1) 
revenue_ar = revenue.values

date, value = np.loadtxt(revenue_ar, delimiter=',', unpack=True,
                                   converters={ 0: bytespdate2num('%Y-%m-%d')})('%Y-%m-%d')})('%Y-%m-%d')})

fig = plt.figure()
ax1 = plt.subplot2grid((6,4), (0,0), rowspan=6, colspan=4)
ax1.plot(date,value)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.show()

但是,这似乎可以使用 revenue.txt:

date, value = np.loadtxt('revenue.txt', delimiter='\t', unpack=True,
                                   converters={0: bytespdate2num('%Y-%m-%d')})

如果我需要进一步澄清我的问题,请告诉我。 提前致谢。


收入1:

[{'date': '2018-03-31', 'value': 247417000000.0},
{'date': '2017-12-30', 'value': 239176000000.0},
{'date': '2017-09-30', 'value': 229234000000.0},
{'date': '2017-07-01', 'value': 223507000000.0},
{'date': '2017-04-01', 'value': 220457000000.0},
{'date': '2016-12-31', 'value': 218118000000.0},
{'date': '2016-09-24', 'value': 215639000000.0},
{'date': '2016-06-25', 'value': 220288000000.0},
{'date': '2016-03-26', 'value': 227535000000.0},
{'date': '2015-12-26', 'value': 234988000000.0},
{'date': '2015-09-26', 'value': 233715000000.0},
{'date': '2015-06-27', 'value': 224337000000.0},
{'date': '2015-03-28', 'value': 212164000000.0},

revenue_ar:

array([['2018-03-31', 247417000000.0],
       ['2017-12-30', 239176000000.0],
       ['2017-09-30', 229234000000.0],
       ['2017-07-01', 223507000000.0],
       ['2017-04-01', 220457000000.0],
       ['2016-12-31', 218118000000.0],
       ['2016-09-24', 215639000000.0],
       ['2016-06-25', 220288000000.0],
       ['2016-03-26', 227535000000.0],
       ['2015-12-26', 234988000000.0],
       ['2015-09-26', 233715000000.0],

收入.txt:

2007-09-29  2.457800e+10
2008-09-27  3.749100e+10
2009-09-26  4.290500e+10
2009-12-26  4.670800e+10
2010-03-27  5.112300e+10
2010-06-26  5.708900e+10
2010-09-25  6.522500e+10
2010-12-25  7.628300e+10
2011-03-26  8.745100e+10
2011-06-25  1.003220e+11
2011-09-24  1.082490e+11

这将是您建议的解决方案。 这很棒,因为它运行流畅。

import time
import urllib.request
from urllib.request import urlopen
import requests
import intrinio
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import datetime

api_username = 'hidden'
api_password = 'hidden'

def grab_intrinio(ticker):
    try:
        revenue_data = requests.get('https://api.intrinio.com/historical_data?    identifier=' + ticker + '&item=totalrevenue', auth=(api_username, api_password))
        revenue1 = revenue_data.json()['data'] 
        revenue = pd.DataFrame(revenue1)
        revenue['date'] = pd.to_datetime(revenue['date'])

        plt.plot(revenue['date'], revenue['value'])

    except Exception as e:
        print('failed in the main loop',str(e))
        pass

grab_intrinio('AAPL')

这会产生如下输出:

**我还有 2 件事要做。 首先,我想绘制另外两个变量(net_income 和 roe)

其次,我的 roe 数据有一个 nm 值,不能转换为浮点数或整数。

我该如何解决这个问题?**

作为最终输出,我想展示一个像这样的图表(我可以自己处理图表和配置细节):

我已经尝试过这条线,但这似乎不适用于显示'list' object has no attribute 'plot'.的错误

fig = plt.figure()

    ax1 = plt.plot(net_income['date'], net_income['value'])
    ax1.plot(net_income['date'], net_income['value'])

    ax2 = plt.plot(revenue['date'], revenue['value'])
    ax2.plot(revenue['date'], revenue['value'])

这个在同一个地块中产生净收入和收入:

plt.plot(net_income['date'], net_income['value'])
plt.plot(revenue['date'], revenue['value'])

块引用

这里是 net_income 和 roe 的代码(与收入格式相同)

net_income_data = requests.get('https://api.intrinio.com/historical_data?identifier=' + ticker + '&item=totalrevenue', auth=(api_username, api_password))
net_income1 = net_income_data.json()['data']
net_income = pd.DataFrame(net_income1)
net_income['date'] = pd.to_datetime(net_income['date'])        

roe_data = requests.get('https://api.intrinio.com/historical_data?identifier=' + ticker + '&item=roe', auth=(api_username, api_password))
roe1 = roe_data.json()['data']
roe = pd.DataFrame(roe1)
roe['date'] = pd.to_datetime(revenue['date'])

这是nm value的roe_date

    date    value
30  2010-09-25  0.352835
31  2010-06-26  0.354701
32  2010-03-27  0.274779
33  2009-12-26  0.261631
34  2009-09-26  0.305356
35  2008-09-27  0.274432
36  2007-09-29  nm

这是roe.dtypes的结果

In: roe.dtypes
Out: date     datetime64[ns]
     value            object
     dtype: object

net_income.dtypesrevenue.dtypes 都产生如下输出:

In: net_income.dtypes(revenue.dtypes)
Out: date     datetime64[ns]
     value           float64
     dtype: object

您对 roe 从对象转换为浮点数的修正有助于绘制图表。当我将函数聚合为最后一步时,我收到invalid syntax 错误,如下所示:

File "<ipython-input-141-537d7c6c91a3>", line 28
    fig axs = plt.subplots(3)

在您的帮助下编写的这个函数。

def grab_intrinio(ticker):
    try:
        net_income_data = requests.get('https://api.intrinio.com/historical_data?identifier=' + ticker + '&item=netincome', auth=(api_username, api_password)) # 
        net_income1 = net_income_data.json()['data']
        net_income = pd.DataFrame(net_income1)
        net_income['date'] = pd.to_datetime(net_income['date'])

        revenue_data = requests.get('https://api.intrinio.com/historical_data?identifier=' + ticker + '&item=totalrevenue', auth=(api_username, api_password))
        revenue1 = revenue_data.json()['data']
        revenue = pd.DataFrame(revenue1)

        revenue['date'] = pd.to_datetime(revenue['date'])
        revenue

        roe_data = requests.get('https://api.intrinio.com/historical_data?identifier=' + ticker + '&item=roe', auth=(api_username, api_password))
        roe1 = roe_data.json()['data']
        roe = pd.DataFrame(roe1)
        roe['date'] = pd.to_datetime(roe['date'])
        roe.index = roe['date']
        roe = roe.drop(columns=['date'])
        nm_idx = roe['value'] =='nm'

        roe.value[nm_idx] = np.nan
        roe.value = roe.value.astype(float)

        fig axs = plt.subplots(3)
        for ax, dat in zip(axs, [net_income, Revenue, roc]):
            ax.plot(dat['date'], dat['value'])

    except exception as e:
        print('failed in the main loop',str(e))
        pass

grab_intrinio('AAPL')    

提前感谢您的帮助。

【问题讨论】:

  • 好的,if I add this line, I get the following error: #*KeyError: 'date' 理解,但请查看错误消息:是否真的是这一行,它会引发错误或这行是否有效,然后您尝试访问日期列 - 这会引发这个错误是因为它之前被成功删除过?
  • 对不起,我还是python新手所以,我需要一些时间来重新审视问题和答案。我已经编辑了我的问题,并通过省略这一行' df = df.drop(columns=['date']) ' 似乎一切运行顺利。
  • 如何解决 roe 数据的 nm 值无法转换为字符串或整数的问题?
  • 感谢您删除数据图像。 roe 是否成功放入数据框中?那你能把roe.dtypes的结果贴出来吗?
  • This would be the solution as you have suggested. This is awesome as it runs smoothly - 怎么能顺利运行,因为您在变量revenue 中读取数据,但是对于绘图,您突然使用dfplt.plot(df['date'], df['value']) 我预计会出错在这里。

标签: python matplotlib numpy-ndarray


【解决方案1】:

np.loadtxt 需要一个文件名或字符串变量,它可以从中解析出数据。这就是为什么它通过告诉它一条路径而不是通过告诉它一个值数组来工作的原因。

所以你显然通过requests.get 获得了有效的 json 数据并通过

revenue1 = revenue_data.json()['data']

并将其放入数据框中

df = pd.DataFrame(revenue1)

这就是它的样子:

In: df.head()
Out: 
         date         value
0  2018-01-31  247417000000
1  2017-12-30  239176000000
2  2017-09-30  229234000000
3  2017-07-01  223507000000

这是检查数据框中列的数据类型的方法:

In: df.dtypes
Out: 
date     object
value     int64
dtype: object

value 是一个整数,这很好,但是date 没有被解析,它只是对象数据,所以让我们解决这个问题:

df['date'] = pd.to_datetime(df['date'])

In: df
Out: 
        date         value
0 2018-01-31  247417000000
1 2017-12-30  239176000000
2 2017-09-30  229234000000
3 2017-07-01  223507000000

In: df.dtypes
Out: 
date     datetime64[ns]
value             int64
dtype: object    df = df.drop(columns=['date'])

现在date 具有正确的数据类型,您可以像这样绘制它

plt.plot(df['date'], df['value'])

但是,如果您将日期作为索引,则可以使其更加方便:

df.index = pd.to_datetime(df['date'])
df = df.drop(columns=['date'])

因为你可以简单地调用

df.plot()

因为 pandas 有一个 matplotlib 接口。

[![在此处输入图片描述][2]][2]

对于你的三重情节,你需要这样的东西:

fig axs = plt.subplots(3)
for ax, dat in zip(axs, [net_income, Revenue, roc]):
    ax.plot(dat['date'], dat['value'])

由于nm-entries,您的某些数据无法转换为浮点数。将它们替换为np.nan,以便绘图命令可以处理它并且您可以使用您的数据:

In: roe
Out: 
          date     value
30  2010-09-25  0.352835
31  2010-06-26  0.354701
32  2010-03-27  0.274779
33  2009-12-26  0.261631
34  2009-09-26  0.305356
35  2008-09-27  0.274432
36  2007-09-29        nm

roe.index = roe['date']
roe = roe.drop(columns=['date'])
nm_idx = roe['value'] =='nm'

roe.value[nm_idx] = np.nan
roe.value = roe.value.astype(float)

In: roe
Out: 
               value
date                
2010-09-25  0.352835
2010-06-26  0.354701
2010-03-27  0.274779
2009-12-26  0.261631
2009-09-26  0.305356
2008-09-27  0.274432
2007-09-29       NaN

In: roe.dtypes
Out: 
value    float64
dtype: object

roe.plot()

【讨论】:

  • 感谢 cmets。您的解决方案返回错误消息:无法将字符串转换为浮点数:'2007-09-29'。我在上面的问题中提供了receiver_ar 和receiver.txt 的样本。您可以通过单击每个单词来查看它们。干杯,
  • 啊,我没有注意到您的数据有日期作为 x 值。首先,让我提一下,请不要将代码或文本文件内容/样本作为图像发布。这对那些想帮助你的人来说并不好,因为没有任何东西可以复制/粘贴来进行测试或检查出了什么问题。对于列表或数组,您可以简单地发布my_list[:5] 的输出,或者对于数据框,有一个不错的小函数df.head()
  • 感谢您的提醒。你有更好的办法来解决这个问题吗?
  • 是的,我现在没有电脑,只有手机。但我会编辑...
  • 感谢编辑的 cmets。但是,当我尝试 df = pd.read_json(revenue_data) 时,会显示以下错误消息: ValueError: Invalid file path or buffer object type:
猜你喜欢
  • 2018-10-10
  • 2021-10-15
  • 1970-01-01
  • 2018-12-19
  • 2011-05-02
  • 1970-01-01
  • 2014-01-28
  • 2016-09-21
  • 1970-01-01
相关资源
最近更新 更多