熊猫引发:AttributeError:模块'pandas.core'没有属性'format'

Jam*_*per 2 python pandas

运行时出现以下错误pd.core.format.header_style = None

AttributeError                            Traceback (most recent call last)
<ipython-input-25-fb23b66754fa> in <module>()
     11 # df1.to_excel(writer, sheet_name='Sheet1')
     12 
---> 13 pd.core.format.header_style = None

AttributeError: module 'pandas.core' has no attribute 'format'
Run Code Online (Sandbox Code Playgroud)

有人知道format搬到哪里吗?

mir*_*ulo 6

您现在正在寻找

pd.formats.format.header_style = None
Run Code Online (Sandbox Code Playgroud)

我相信,从版本开始0.18.1。请参阅问题CLN&REORG core / common.py#12503


编辑(版本> = 0.20

正如Jeff所提到的,这不是公共财产,因此很容易四处走动。现在可以在中找到pandas.io.formats.excel它,您必须将其导入。

如果您想处理到目前为止从不同版本进行的访问(同样容易受到更改),那么针对此不兼容问题的适应可能看起来像

import packaging.version
import pandas
import pandas.io.formats.excel

def get_format_module():
    version = packaging.version.parse(pandas.__version__)
    if version < packaging.version.parse('0.18'):
        return pandas.core.format
    elif version < packaging.version.parse('0.20'):
        return pandas.formats.format
    else:
        return pandas.io.formats.excel
Run Code Online (Sandbox Code Playgroud)

  • 是的,就是这样。难道你不喜欢东西被移动吗? (3认同)