浅谈pandas关于查看库或依赖库版本的API原理
作者:mighty13 发布时间:2023-10-14 04:54:13
概述
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}")
来源:https://blog.csdn.net/mighty13/article/details/119974352


猜你喜欢
- 找了半天,以为numpy的where函数像matlab 的find函数一样好用,能够返回一个区间内的元素索引位置。结果没有。。(也可能是我没
- 本文实例讲述了Python 文件管理的方法。分享给大家供大家参考,具体如下:一、Python中的文件管理文件管理是很多应用程序的基本功能和重
- 一、picklepickle模块用来实现python对象的序列化和反序列化。通常地pickle将python对象序列化为二进制流或文件。&n
- MySQL Order By keyword是用来给记录中的数据进行分类的。MySQL Order By Keyword根据关键词分类ORD
- 1.案例要求:"""有列表["a", "d", "f&quo
- 我想大多写web的朋友应该和我一样,正则是不可少的,可是每次到用时去百度一下,也麻烦,存在电脑里也得找半天~换了电脑还是得靠google了~
- UCD介绍UCD是Unicode字符数据库(Unicode Character DataBase)的缩写。UCD由一些描述Unicode字符
- 一、Mysql SQL Mode简介通常来说MySQL服务器能够工作在不同的SQL模式下,并能针对不同的客户端以不同的方式应用这些模式。这样
- 通常情况下,我们想构建一张表单时会在模板文件login.html中写入<form action="/your-name/&q
- 这篇文章主要介绍了pyinstaller还原python代码过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- Microsoft SQL Server 2008将包含用于合并两个行集(rowset)数据的新句法。根据一个源数据表对另一个数据表进行确定
- 我在配置mysql时将配置文件中的默认存储引擎设定为了InnoDB。今天查看了MyISAM与InnoDB的区别,在该文中的第七条“MyISA
- 本文实例讲述了Python迭代器定义与简单用法。分享给大家供大家参考,具体如下:一、什么是迭代器迭代,顾名思义就是重复做一些事很多次(就现在
- 我们先来看一下运行图下面我们来看源代码:<?php//抓取抖音的接口数据global $nCov_data;$nCov_data[
- python logging日志模块的详解日志级别日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR C
- 1.创建tfrecordtfrecord支持写入三种格式的数据:string,int64,float32,以列表的形式分别通过tf.trai
- Softmax回归函数是用于将分类结果归一化。但它不同于一般的按照比例归一化的方法,它通过对数变换来进行归一化,这样实现了较大的值在归一化过
- pom.xml文件中引入如下内容<dependency><groupId>com.github.ulisesbocc
- Twig是一款快速、安全、灵活的PHP模板引擎,它内置了许多filter和tags,并且支持模板继承,能让你用最简洁的代码来描述你的模板。他
- 我们使用编辑器的时候,想要在其中添加一个Django项目,这样就能在里面做一些想要的操作。有些人还没有对Django进行安装,这里直接用命令