调用theseus_growth报object无dtype属性 升级pandas无效
theseus_growth 报错 AttributeError 修复方案
问题描述
调用Python第三方库theseus_growth开展开发时触发如下报错:
AttributeError: type object 'object' has no attribute 'dtype'
此前在其他设备上遇到完全相同的错误,执行conda install -c anaconda pandas升级pandas即可解决,但当前环境存在两个异常:
- 直接在Notebook代码单元执行上述conda命令会抛出
SyntaxError: invalid syntax错误 - 命令前加Notebook专用的
!前缀执行时,安装进程长时间挂起无进展
当前使用环境为Anaconda navigator 1.9.6,因高版本Anaconda无法兼容theseus_growth库,未升级Anaconda版本。已尝试执行以下命令均未解决问题:
!python -m pip install --upgrade pandas !pip3 install --upgrade pandas !C:\Users\jamit\AppData\Local\Programs\Python\Python310\python.exe -m pip install --upgrade pip
完整错误堆栈
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-9-30b0b570169e> in <module> 2 cohorts = [561, 670, 613, 543, 491 ] 3 ----> 4 tiktok_DAU = my_th.project_cohorted_DAU( profile = tiktok, periods = 50, cohorts = cohorts, start_date = 1 ) 5 6 print( tiktok_DAU ) ~\Anaconda30000\lib\site-packages\theseus_growth\__init__.py in project_cohorted_DAU(self, profile, periods, cohorts, DAU_target, DAU_target_timeline, start_date) 95 def project_cohorted_DAU(self, profile, periods, cohorts, DAU_target=None, DAU_target_timeline=None, start_date=1): 96 return cohort_projections.project_cohorted_DAU( ---> 97 profile, periods, cohorts, DAU_target, DAU_target_timeline, start_date 98 ) 99 ~\Anaconda30000\lib\site-packages\theseus_growth\cohort_projections.py in project_cohorted_DAU(profile, periods, cohorts, DAU_target, DAU_target_timeline, start_date) 231 232 # create the blank dataframe that will contain the forward_DAU ---> 233 forward_DAU = pd.DataFrame(columns=['cohort_date'] + dates) 234 # build the initial forward DAU from the cohorts 235 for cohort in cohorts: ~\Anaconda30000\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy) 409 ValueError: columns overlap but no suffix specified: 410 Index(['value'], dtype='object') ---> 411 412 >>> df1 = pd.DataFrame({'a': ['foo', 'bar'], 'b': [1, 2]}) 413 >>> df2 = pd.DataFrame({'a': ['foo', 'baz'], 'c': [3, 4]}) ~\Anaconda30000\lib\site-packages\pandas\core\internals\construction.py in init_dict(data, index, columns, dtype) 240 arrays = [arr.copy() for arr in arrays] 241 new_mgr = ArrayManager(arrays, [mgr.axes[1], mgr.axes[0]]) ---> 242 else: 243 array = mgr.internal_values() 244 if copy: ~\Anaconda30000\lib\site-packages\pandas\core\dtypes\cast.py in construct_1d_arraylike_from_scalar(value, length, dtype) 1219 def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> ArrayLike: 1220 """ -> 1221 Cast array (ndarray or ExtensionArray) to the new dtype. 1222 1223 Parameters AttributeError: type object 'object' has no attribute 'dtype'
修复步骤
报错核心原因是当前环境的pandas版本过高,和老旧的theseus_growth库存在API兼容问题,之前能通过conda安装anaconda源的pandas解决,本质是anaconda源默认安装的是适配老版本依赖的pandas,而非最新版。之前操作无效的原因有三个:一是在Notebook代码单元执行conda命令本身存在环境识别问题,二是默认境外源下载速度慢导致进程挂起,三是误用了系统独立安装的Python3.10的pip,和Anaconda环境完全隔离,安装的包不会生效。
按以下步骤操作即可修复:
- 完全关闭当前运行的Jupyter Notebook,从开始菜单打开Anaconda Prompt(Anaconda专属命令行,不要用系统cmd)。
- 如果你使用的是自定义conda虚拟环境,先执行命令激活对应环境;如果用默认base环境可跳过此步:
conda activate 你的虚拟环境名称 - 先配置国内清华镜像源,解决下载挂起问题,依次执行:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes - 安装和theseus_growth兼容的pandas版本,不要安装最新版,指定0.25.3版本即可:
conda install pandas=0.25.3 -y - 如果conda安装执行失败,直接在当前Anaconda Prompt窗口执行pip安装命令,同样指定版本、用国内源加速:
pip install pandas==0.25.3 -i https://pypi.tuna.tsinghua.edu.cn/simple - 安装完成后执行
pip show pandas确认版本为0.25.x,重新启动Jupyter Notebook、重启内核后运行代码,报错即可消失。
内容的提问来源于stack exchange,提问作者Omar
相关产品推荐
相关产品推荐

