gpt4 book ai didi

python - pandas plotting.py 代码中缺少全局转换对象?

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:27 24 4
gpt4 key购买 nike

我正在尝试使用 pandas 完成一些示例。我从 Yahoo 加载一个 DataFrame,将其导出到一个文件,将其读入 DataFrame,然后创建我想要绘制的系列。当我使用情节代码时,它会抛出错误。错误是 NameError: global name 'conv' is not defined。代码和完整错误如下。

import datetime
import pandas as pd
import pandas.io.data
from pandas import Series, DataFrame

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rc('figure', figsize=(8, 7))


aapl = pd.io.data.get_data_yahoo('AAPL',
start=datetime.datetime(2006, 10, 1),
end=datetime.datetime(2014, 4, 10))

aapl.to_csv('data/aapl_ohlc.csv')

df = pd.read_csv('data/aapl_ohlc.csv', index_col='Date', parse_dates=True)

close_px = df['Adj Close']
mavg = pd.rolling_mean(close_px, 40)

close_px.plot(label='AAPL')
mavg.plot(label='mavg')
plt.legend()

它抛出以下错误

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-58-34eecf8ee548> in <module>()
1 from pylab import *
----> 2 close_px.plot(label='AAPL')
3 mavg.plot(label='mavg')
4 plt.legend()

C:\Python33\lib\site-packages\pandas\tools\plotting.py in plot_series(series, label, kind, use_index, rot, xticks, yticks, xlim, ylim, ax, style, grid, legend, logx, logy, secondary_y, **kwds)
1827 secondary_y=secondary_y, **kwds)
1828
-> 1829 plot_obj.generate()
1830 plot_obj.draw()
1831

C:\Python33\lib\site-packages\pandas\tools\plotting.py in generate(self)
903 self._compute_plot_data()
904 self._setup_subplots()
--> 905 self._make_plot()
906 self._post_plot_logic()
907 self._adorn_subplots()

C:\Python33\lib\site-packages\pandas\tools\plotting.py in _make_plot(self)
1354
1355 if self._is_datetype():
-> 1356 left, right = _get_xlim(lines)
1357 ax.set_xlim(left, right)
1358

C:\Python33\lib\site-packages\pandas\tools\plotting.py in _get_xlim(lines)
2507 for l in lines:
2508 x = l.get_xdata()
-> 2509 left = min(_maybe_convert_date(x[0]), left)
2510 right = max(_maybe_convert_date(x[-1]), right)
2511 return left, right

C:\Python33\lib\site-packages\pandas\tools\plotting.py in _maybe_convert_date(x)
2514 def _maybe_convert_date(x):
2515 if not com.is_integer(x):
-> 2516 conv_func = conv._dt_to_float_ordinal
2517 if isinstance(x, datetime.time):
2518 conv_func = conv._to_ordinalf

NameError: global name 'conv' is not defined

根据评论更新

代码

import pandas.tseries.converter
print(dir(pandas.tseries.converter))

输出

['AutoLocator', 'DatetimeConverter', 'Formatter', 'FreqGroup', 'HOURS_PER_DAY', 'Index', 'Locator', 'MINUTES_PER_DAY', 'MUSECONDS_PER_DAY', 'MilliSecondLocator', 'PandasAutoDateFormatter', 'PandasAutoDateLocator', 'Period', 'PeriodConverter', 'PeriodIndex', 'SECONDS_PER_DAY', 'TimeConverter', 'TimeFormatter', 'TimeSeries_DateFormatter', 'TimeSeries_DateLocator', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_annual_finder', '_daily_finder', '_dt_to_float_ordinal', '_from_ordinal', '_get_default_annual_spacing', '_monthly_finder', '_quarterly_finder', '_to_ordinalf', 'com', 'compat', 'date_range', 'dates', 'datetime', 'frequencies', 'get_datevalue', 'get_finder', 'has_level_label', 'lib', 'lrange', 'nonsingular', 'np', 'period_break', 'pydt', 'register', 'relativedelta', 'time2num', 'timedelta', 'tools', 'units'] 

最佳答案

启动一个新的 IPython Notebook session 将其清除。我不太确定自己对自己做了什么,但吸取了教训。

有一点需要补充

from imp import reload

到文件 lib\site-packages\pandas\tools\plotting.py

关于python - pandas plotting.py 代码中缺少全局转换对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com