首页 > 解决方案 > Pandas 导入错误“ImportError: cannot import name 'FrameOrSeriesUnion' from 'pandas._typing'”

问题描述

当我尝试 import pandas as pd 时,它给出了这个错误。我不能导入熊猫。我重新安装了 pandas,但它是一样的。我尝试运行 promt 和 jupyter notebook。我正在使用 conda env 和 pycharm。我尝试了另一台电脑,但它是一样的。也许有些东西不见了?我应该怎么办 ?这是我的代码:

import pandas as pd
from neuralprophet import NeuralProphet
from matplotlib import pyplot as plt
import pickle

df = pd.read_csv('weatherAUS.csv')
df.head()
df.Location.unique()
df.columns
melb = df[df['Location']=='Melbourne']
melb['Date'] = pd.to_datetime(melb['Date'])
melb.head()
plt.plot(melb['Date'], melb['Temp3pm'])
plt.show()
melb['Year'] = melb['Date'].apply(lambda x: x.year)
melb = melb[melb['Year']<=2015]
plt.plot(melb['Date'], melb['Temp3pm'])
plt.show()
data = melb[['Date', 'Temp3pm']]
data.dropna(inplace=True)
data.columns = ['ds', 'y']
data.head()
#model eğitme
m = NeuralProphet()
model = m.fit(data, freq='D', epochs=1000)
#forecast
future = m.make_future_dataframe(data, periods=900)
forecast = m.predict(future)
forecast.head()
plot1 = m.plot(forecast)
plt2 = m.plot_components(forecast)
#save model
with open('saved_model.pkl', "wb") as f:
    pickle.dump(m, f)
del m
with open('saved_model.pkl', "rb") as f:
    m = pickle.load(f)
future = m.make_future_dataframe(data, periods=900)
forecast = m.predict(future)
forecast.head()
plot1 = m.plot(forecast)

错误:

Traceback (most recent call last):
  File "C:/Users/Lenovo/PycharmProjects/pythonProject/a.py", line 1, in <module>
    import pandas as pd
  File "C:\Users\Lenovo\anaconda3\envs\pythonProject\lib\site-packages\pandas\__init__.py", line 51, in <module>
    from pandas.core.api import (
  File "C:\Users\Lenovo\anaconda3\envs\pythonProject\lib\site-packages\pandas\core\api.py", line 14, in <module>
    from pandas.core.algorithms import factorize, unique, value_counts
  File "C:\Users\Lenovo\anaconda3\envs\pythonProject\lib\site-packages\pandas\core\algorithms.py", line 15, in <module>
    from pandas._typing import AnyArrayLike, ArrayLike, DtypeObj, FrameOrSeriesUnion
ImportError: cannot import name 'FrameOrSeriesUnion' from 'pandas._typing' (C:\Users\Lenovo\anaconda3\envs\pythonProject\lib\site-packages\pandas\_typing.py)

Process finished with exit code 1

标签: pythonpandasimportcompiler-errorsanaconda

解决方案


我解决了这个错误。Pandas 需要 NumPy、python-dateutil 和 pytz。我没有安装 dateutil 和 pytz。所以我安装了 dateutil 和 pytz。


推荐阅读