淺談pandas關(guān)于查看庫或依賴庫版本的API原理
概述
pandas中與庫版本或依賴庫版本相關(guān)的API主要有以下4個(gè):
pandas.__version__:查看pandas簡(jiǎn)要版本信息。pandas.__git_version__:查看pandasgit版本信息。pandas._version.get_versions():查看pandas詳細(xì)版本信息。pandas.show_versions():查看pandas及其依賴庫的版本信息。
上述API的運(yùn)行效果如下:
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。根據(jù)源碼可知,該模塊以JSON字符串形式存儲(chǔ)版本信息,通過get_versions()返回字典形式的詳細(xì)版本信息。
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。根據(jù)源碼可知,pandas.__version__和pandas.__git_version__源自于pandas._version.get_versions()的返回值。
生成這兩個(gè)之后,刪除了get_versions、v兩個(gè)命名空間,因此不能使用pandas.get_versions()或pandas.v形式查看版本信息。
相關(guān)源碼:
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()原理
根據(jù)pandas包根目錄下的__init__.py源碼可知,通過from pandas.util._print_versions import show_versions重構(gòu)命名空間,pandas.show_versions()的源代碼位于pandas包util目錄下的_print_versions.py模塊。
根據(jù)源碼可知,pandas.show_versions()的參數(shù)取值有3種情況:
False:打印輸出類表格形式的依賴庫版本信息。True:打印輸出JSON字符串形式的依賴庫版本信息。字符串:參數(shù)被認(rèn)為是文件路徑,版本信息以JSON形式寫入該文件。
注意!pandas.show_versions()沒有返回值即None。
pandas.show_versions()不同參數(shù)輸出結(jié)果
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")

相關(guān)源碼:
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}")
到此這篇關(guān)于淺談pandas關(guān)于查看庫或依賴庫版本的API原理的文章就介紹到這了,更多相關(guān)pandas 依賴庫API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)棧的方法詳解【基于數(shù)組和單鏈表兩種方法】
這篇文章主要介紹了Python實(shí)現(xiàn)棧的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python基于數(shù)組和單鏈表兩種方法定義棧的相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
yolov5中anchors設(shè)置實(shí)例詳解
在YOLOV5算法之中,針對(duì)不同的數(shù)據(jù)集,一般會(huì)預(yù)先設(shè)置固定的Anchor,下面這篇文章主要給大家介紹了關(guān)于yolov5中anchors設(shè)置的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Python實(shí)現(xiàn)識(shí)別XSS漏洞的方法詳解
XSS(跨站腳本攻擊)作為一種常見的網(wǎng)絡(luò)安全漏洞,經(jīng)常被黑客用來攻擊網(wǎng)站。這篇文章主要介紹了如何利用Python 識(shí)別 XSS 漏洞,需要的可以參考一下2023-02-02
Python使用Selenium WebDriver的入門介紹及安裝教程(最新推薦)
這篇文章主要介紹了Python使用Selenium WebDriver的入門介紹及安裝教程,本文使用環(huán)境為python3.11+win10 64位+firefox瀏覽器,所以本文使用的瀏覽器驅(qū)動(dòng)是Firefox的geckodriver ,如果你使用的是其他瀏覽器,那么選擇自己對(duì)應(yīng)的瀏覽器驅(qū)動(dòng)程序即可,需要的朋友可以參考下2023-04-04
python的getattr和getattribute攔截內(nèi)置操作實(shí)現(xiàn)
在Python中,getattr和getattribute是用于動(dòng)態(tài)屬性訪問和自定義屬性訪問行為的重要工具,本文主要介紹了python的getattr和getattribute攔截內(nèi)置操作實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
ubuntu系統(tǒng)下 python鏈接mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了ubuntu系統(tǒng)下 python鏈接mysql數(shù)據(jù)庫的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
簡(jiǎn)單實(shí)現(xiàn)python爬蟲功能
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單爬蟲功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2015-12-12
python實(shí)現(xiàn)簡(jiǎn)單socket通信的方法
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單socket通信的方法,結(jié)合實(shí)例形式分析了socket通信服務(wù)端與客戶端的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04

