【问题标题】:Issue accessing a URL with error "urlopen error Tunnel connection failed" using pandas and matplotlib使用 pandas 和 matplotlib 访问带有错误“urlopen 错误隧道连接失败”的 URL 的问题
【发布时间】:2021-01-26 02:14:17
【问题描述】:

网络上的数据源/文件位置是:https://www.newyorkfed.org/medialibrary/media/survey/empire/data/esms_seasonallyadjusted_diffusion.csv 但是,由于存在连接问题,我在本地保存了它('esms_seasonallyadjusted_diffusion.csv') 无论如何都是为了速度而做的最好的事情 我也把它保存到了github: https://github.com/me50/hlar65/blob/master/ESMS_SeasonallyAdjusted_Diffusion.csv')

2 个问题:

  1. 尝试访问网络链接时(即使单击它会下载文件)我收到连接错误:“URLError:
  2. 我的代码(我是初学者!)看起来很笨拙。有没有更干净更好的表达方式?

感谢大家的帮助 '''

import pandas as pd
import numpy as np
from pandas.plotting import scatter_matrix
import scipy as sp
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sn


df = dd.read_csv('https://www.newyorkfed.org/medialibrary/media/survey/empire/data/esms_seasonallyadjusted_diffusion.csv')
 
df = df.rename(columns={'surveyDate':'Date',
                        'GACDISA': 'IndexAll', 
                        'NECDISA': 'NumberofEmployees',
                        'NOCDISA': 'NewOrders',
                        'PPCDISA': 'PricesPaid',
                        'PRCDISA': 'PricesReceived'})
headers = df.columns

df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)

IndexAll = df['IndexAll']
NumberofEmployees = df['NumberofEmployees']
NewOrders = df['NewOrders']
PricesReceived = df['PricesReceived']

data = df[['IndexAll', 'NumberofEmployees', 'NewOrders', 'PricesReceived']]
data2 = data.copy()

ds = data2
FS_A = 14
FS_L = 16
FS_T = 20
FS_MT = 25

fig, ((ax0, ax1), (ax2,ax3)) = plt.subplots(nrows=2, ncols=2, figsize=(20,15))

# density=True : probability density i.e. prb of an outcome; False = actual # of frequency

ds['IndexAll'].plot(ax=ax0, color='red')
ax0.set_title('New York Empire Manufacturing Index', fontsize = FS_T)
ax0.set_ylabel('Date', fontsize = FS_A) 
ax0.set_xlabel('Empire Index', fontsize = FS_L) 
ax0.tick_params(labelsize=FS_A)

ds['NumberofEmployees'].plot(ax=ax1, color='blue')
ax1.set_title('Empire: Number of Employees', fontsize = FS_T)
ax1.set_ylabel('Date', fontsize = FS_L) 
ax1.set_xlabel('Number of Employees', fontsize = FS_L) 
ax1.tick_params(labelsize=FS_A)

ds['NewOrders'].plot(ax=ax2, color='green')
ax2.set_title('Empire: New Orders', fontsize = FS_T)
ax2.set_ylabel('Date', fontsize = FS_L) 
ax2.set_xlabel('New Orders', fontsize = FS_L) 
ax2.tick_params(labelsize=FS_A)

ds['PricesReceived'].plot(ax=ax3, color='black')
ax3.set_title('Empire: Prices Received', fontsize = FS_T)
ax3.set_ylabel('Date', fontsize = FS_L) 
ax3.set_xlabel('Prices Received', fontsize = FS_L) 
ax3.tick_params(labelsize=FS_A)

fig.tight_layout()
fig.suptitle('New York Manufacturing Index Main Components - Showing the Depths of COVD19 in 2020', fontsize = FS_MT)
fig.tight_layout()
fig.subplots_adjust(top=0.88)
fig.subplots_adjust(bottom = -0.2) 
fig.savefig("Empire.png")
plt.show()

'''

【问题讨论】:

  • 第一个问题不可重现df = pd.read_csv('https://www.newyorkfed.org/medialibrary/media/survey/empire/data/esms_seasonallyadjusted_diffusion.csv') 可以毫无问题地访问 csv 文件。第二个问题与您的previous question 重复。 SO问题应该集中在一个问题上,而不是多个问题上。另外,不要重复问同一个问题。 How to ask a good question
  • 这能回答你的问题吗? How to plot columns from a dataframe, as subplots

标签: pandas matplotlib urlopen failed-to-connect


【解决方案1】:

对于第一个问题,您是否设置了任何代理?我认为它来自代理设置。

关于第二个,我可以做一些清理工作,但它非常依赖于开发人员的代码风格。您可以用几种不同的方式编写脚本。

注意:

  • 您可以在 read_csv 调用中解析日期
  • 使用括号一步完成您想要使用 df 执行的所有操作
  • 您可以为参数定义数组并在 for 循环中绘制所有图
df = (
    pd
    .read_csv('https://www.newyorkfed.org/medialibrary/media/survey/empire/data/esms_seasonallyadjusted_diffusion.csv',
             parse_dates=['surveyDate'])
    .rename(columns={'surveyDate':'Date',
                        'GACDISA': 'IndexAll', 
                        'NECDISA': 'NumberofEmployees',
                        'NOCDISA': 'NewOrders',
                        'PPCDISA': 'PricesPaid',
                        'PRCDISA': 'PricesReceived'})
    .set_index('Date')
)

FS_A = 14
FS_L = 16
FS_T = 20
FS_MT = 25

titles = ['New York Empire Manufacturing Index','Empire: Number of Employees','Empire: New Orders','Empire: Prices Received']
xlabels = ['Empire Index','Number of Employees','New Orders','Prices Received']
colors=['red','blue','green','black']
columns = ['IndexAll', 'NumberofEmployees', 'NewOrders', 'PricesReceived']
ds = df[columns]
k=0
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(20,15))
for i in range(2):
    for j in range(2):
        ds[columns[k]].plot(ax=axes[i][j], color=colors[k])
        axes[i][j].set_title(titles[k], fontsize = FS_T)
        axes[i][j].set_ylabel('Date', fontsize = FS_A) 
        axes[i][j].set_xlabel(xlabels[k], fontsize = FS_L) 
        axes[i][j].tick_params(labelsize=FS_A)
        k+=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2017-11-21
    • 2015-02-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多