pandas 依赖库API
mighty13 人气:0概述
pandas
中与库版本或依赖库版本相关的API主要有以下4个:
pandas.__version__
:查看pandas
简要版本信息。pandas.__git_version__
:查看pandas
git版本信息。pandas._version.get_versions()
:查看pandas
详细版本信息。pandas.show_versions()
:查看pandas
及其依赖库的版本信息。
上述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_versions
、v
两个命名空间,因此不能使用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()
的源代码位于pandas
包util
目录下的_print_versions.py
模块。
根据源码可知,pandas.show_versions()
的参数取值有3种情况:
False
:打印输出类表格形式的依赖库版本信息。True
:打印输出JSON字符串形式的依赖库版本信息。字符串
:参数被认为是文件路径,版本信息以JSON形式写入该文件。
注意!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}")
加载全部内容