亲宝软件园·资讯

展开

pandas 依赖库API

mighty13 人气:0

概述

pandas中与库版本或依赖库版本相关的API主要有以下4个:

上述API的运行效果如下:

In [1]: import pandas as pd

In [2]: pd.__version__
Out[2]: '1.1.3'

In [3]: pd.__git_version__
Out[3]: 'db08276bc116c438d3fdee492026f8223584c477'

In [4]: pd._version.get_versions()
Out[4]:
{'dirty': False,
 'error': None,
 'full-revisionid': 'db08276bc116c438d3fdee492026f8223584c477',
 'version': '1.1.3'}

In [5]: pd.show_versions(True)
{'system': {'commit': 'db08276bc116c438d3fdee492026f8223584c477', 'python': '3.7.2.final.0', 'python-bits': 64, 'OS': 'Windows', 'OS-release': '10', 'Version': '10.0.17763', 'machine': 'AMD64', 'processor': 'Intel64 Family 6 Model 94 Stepping 3, GenuineIntel', 'byteorder': 'little', 'LC_ALL': None, 'LANG': None, 'LOCALE': {'language-code': None, 'encoding': None}}, 'dependencies': {'pandas': '1.1.3', 'numpy': '1.20.1', 'pytz': '2019.2', 'dateutil': '2.8.0', 'pip': '19.3.1', 'setuptools': '51.1.0.post20201221', 'Cython': None, 'pytest': None, 'hypothesis': None, 'sphinx': None, 'blosc': None, 'feather': None, 'xlsxwriter': '3.0.1', 'lxml.etree': '4.4.2', 'html5lib': '1.1', 'pymysql': '0.9.3', 'psycopg2': None, 'jinja2': '2.11.2', 'IPython': '7.11.1', 'pandas_datareader': None, 'bs4': '4.9.3', 'bottleneck': None, 'fsspec': None, 'fastparquet': None, 'gcsfs': None, 'matplotlib': '3.4.1', 'numexpr': None, 'odfpy': None, 'openpyxl': '2.6.2', 'pandas_gbq': None, 'pyarrow': None, 'pytables': None, 'pyxlsb': None, 's3fs': None, 'scipy': '1.2.1', 'sqlalchemy': '1.4.18', 'tables': None, 'tabulate': None, 'xarray': None, 'xlrd': '1.2.0', 'xlwt': '1.3.0', 'numba': '0.52.0'}}

pandas._version.get_versions()、pandas.__version__和pandas.__git_version__原理

pandas._version.get_versions()

pandas._version.get_versions()源代码位于pandas包根目录下的_version.py。根据源码可知,该模块以JSON字符串形式存储版本信息,通过get_versions()返回字典形式的详细版本信息。

pandas/_version.py源码

from warnings import catch_warnings
with catch_warnings(record=True):
    import json
import sys

version_json = '''
{
 "dirty": false,
 "error": null,
 "full-revisionid": "db08276bc116c438d3fdee492026f8223584c477",
 "version": "1.1.3"
}
'''  # END VERSION_JSON


def get_versions():
    return json.loads(version_json)

pandas.__version__pandas.__git_version__

pandas.__version__pandas.__git_version__源代码位于pandas包根目录下的__init__.py。根据源码可知,pandas.__version__pandas.__git_version__源自于pandas._version.get_versions()的返回值。

生成这两个之后,删除了get_versionsv两个命名空间,因此不能使用pandas.get_versions()pandas.v形式查看版本信息。

相关源码:

from ._version import get_versions

v = get_versions()
__version__ = v.get("closest-tag", v["version"])
__git_version__ = v.get("full-revisionid")
del get_versions, v

pandas.show_versions()原理

根据pandas包根目录下的__init__.py源码可知,通过from pandas.util._print_versions import show_versions重构命名空间,pandas.show_versions()的源代码位于pandasutil目录下的_print_versions.py模块。

根据源码可知,pandas.show_versions()的参数取值有3种情况:

注意!pandas.show_versions()没有返回值即None

pandas.show_versions()不同参数输出结果

In [5]: pd.show_versions(True)
{'system': {'commit': 'db08276bc116c438d3fdee492026f8223584c477', 'python': '3.7.2.final.0', 'python-bits': 64, 'OS': 'Windows', 'OS-release': '10', 'Version': '10.0.17763', 'machine': 'AMD64', 'processor': 'Intel64 Family 6 Model 94 Stepping 3, GenuineIntel', 'byteorder': 'little', 'LC_ALL': None, 'LANG': None, 'LOCALE': {'language-code': None, 'encoding': None}}, 'dependencies': {'pandas': '1.1.3', 'numpy': '1.20.1', 'pytz': '2019.2', 'dateutil': '2.8.0', 'pip': '19.3.1', 'setuptools': '51.1.0.post20201221', 'Cython': None, 'pytest': None, 'hypothesis': None, 'sphinx': None, 'blosc': None, 'feather': None, 'xlsxwriter': '3.0.1', 'lxml.etree': '4.4.2', 'html5lib': '1.1', 'pymysql': '0.9.3', 'psycopg2': None, 'jinja2': '2.11.2', 'IPython': '7.11.1', 'pandas_datareader': None, 'bs4': '4.9.3', 'bottleneck': None, 'fsspec': None, 'fastparquet': None, 'gcsfs': None, 'matplotlib': '3.4.1', 'numexpr': None, 'odfpy': None, 'openpyxl': '2.6.2', 'pandas_gbq': None, 'pyarrow': None, 'pytables': None, 'pyxlsb': None, 's3fs': None, 'scipy': '1.2.1', 'sqlalchemy': '1.4.18', 'tables': None, 'tabulate': None, 'xarray': None, 'xlrd': '1.2.0', 'xlwt': '1.3.0', 'numba': '0.52.0'}}

In [6]: pd.show_versions()


INSTALLED VERSIONS
------------------
commit           : db08276bc116c438d3fdee492026f8223584c477
python           : 3.7.2.final.0
python-bits      : 64
OS               : Windows
OS-release       : 10
Version          : 10.0.17763
machine          : AMD64
processor        : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder        : little
LC_ALL           : None
LANG             : None
LOCALE           : None.None

pandas           : 1.1.3
numpy            : 1.20.1
pytz             : 2019.2
dateutil         : 2.8.0
pip              : 19.3.1
setuptools       : 51.1.0.post20201221
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : 3.0.1
lxml.etree       : 4.4.2
html5lib         : 1.1
pymysql          : 0.9.3
psycopg2         : None
jinja2           : 2.11.2
IPython          : 7.11.1
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : 3.4.1
numexpr          : None
odfpy            : None
openpyxl         : 2.6.2
pandas_gbq       : None
pyarrow          : None
pytables         : None
pyxlsb           : None
s3fs             : None
scipy            : 1.2.1
sqlalchemy       : 1.4.18
tables           : None
tabulate         : None
xarray           : None
xlrd             : 1.2.0
xlwt             : 1.3.0
numba            : 0.52.0

In [7]: pd.show_versions("./version.json")

在这里插入图片描述

相关源码:

def show_versions(as_json: Union[str, bool] = False) -> None:
    """
    Provide useful information, important for bug reports.

    It comprises info about hosting operation system, pandas version,
    and versions of other installed relative packages.

    Parameters
    ----------
    as_json : str or bool, default False
        * If False, outputs info in a human readable form to the console.
        * If str, it will be considered as a path to a file.
          Info will be written to that file in JSON format.
        * If True, outputs info in JSON format to the console.
    """
    sys_info = _get_sys_info()
    deps = _get_dependency_info()

    if as_json:
        j = dict(system=sys_info, dependencies=deps)

        if as_json is True:
            print(j)
        else:
            assert isinstance(as_json, str)  # needed for mypy
            with codecs.open(as_json, "wb", encoding="utf8") as f:
                json.dump(j, f, indent=2)

    else:
        assert isinstance(sys_info["LOCALE"], dict)  # needed for mypy
        language_code = sys_info["LOCALE"]["language-code"]
        encoding = sys_info["LOCALE"]["encoding"]
        sys_info["LOCALE"] = f"{language_code}.{encoding}"

        maxlen = max(len(x) for x in deps)
        print("\nINSTALLED VERSIONS")
        print("------------------")
        for k, v in sys_info.items():
            print(f"{k:<{maxlen}}: {v}")
        print("")
        for k, v in deps.items():
            print(f"{k:<{maxlen}}: {v}")

加载全部内容

相关教程
猜你喜欢
用户评论