核心内容
pandas 的选项系统将全局设置划分为 display、mode、compute、plotting、future 五大类别,每类下包含若干具体选项。
一、display —— 显示控制
控制 DataFrame / Series 在控制台中的输出形式。
| 选项 | 默认值 | 说明 |
|---|---|---|
display.max_rows | 60 | 最大显示行数,超出则折叠为头尾两段 |
display.max_columns | 20 | 最大显示列数,超出则中间省略 |
display.width | 80 | 控制台显示宽度(字符数) |
display.max_colwidth | 50 | 列内容最大宽度(字符数) |
display.precision | 6 | 浮点数显示精度 |
display.float_format | None | 浮点数格式化函数(如 lambda x: f'{x:,.2f}') |
display.colheader_justify | ’right’ | 列标题对齐方式:left / right / center |
display.expand_frame_repr | True | 是否跨多行显示宽 DataFrame |
display.show_dimensions | ’truncate’ | 是否显示维度信息(rows × columns) |
display.unicode | — | Unicode 设置(如 east_asian_width) |
display.encoding | ’utf-8’ | 输出编码 |
示例
import pandas as pd
pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', None) # 显示所有列
pd.set_option('display.width', 120)
pd.set_option('display.precision', 3)
pd.set_option('display.colheader_justify', 'center')二、mode —— 模式控制
| 选项 | 默认值 | 说明 |
|---|---|---|
mode.chained_assignment | ’warn’ | 链式赋值警告级别:‘warn’ / ‘raise’ / None |
mode.data_manager | ’block’ | 数据管理器:‘block’(默认)/ ‘array’ |
mode.use_inf_as_na | False | 是否将 inf / -inf 视为缺失值 |
mode.copy_on_write | False | 启用全局 Copy-on-Write 行为 |
mode.string_storage | ’python’ | 字符串存储方式:‘python’ / ‘pyarrow’ |
pd.set_option('mode.chained_assignment', 'raise') # 链式赋值直接抛异常
pd.set_option('mode.use_inf_as_na', True) # inf 视为缺失三、compute —— 计算加速
| 选项 | 默认值 | 说明 |
|---|---|---|
compute.use_bottleneck | True | 使用 bottleneck 加速聚合运算 |
compute.use_numba | False | 使用 numba JIT 加速部分运算 |
pd.set_option('compute.use_numba', True)注意
compute.use_numba需要安装numba,且仅部分函数支持 numba 加速。
四、plotting —— 绘图配置
| 选项 | 默认值 | 说明 |
|---|---|---|
plotting.backend | ’matplotlib’ | 绘图后端,可切换为 ‘plotly’、‘html’ 等 |
plotting.matplotlib.register_converters | True | 是否注册 matplotlib 时间转换器 |
pd.set_option('plotting.backend', 'plotly')五、future —— 未来行为开关
| 选项 | 默认值 | 说明 |
|---|---|---|
future.infer_string | False | 自动将 object 列推断为 string 类型 |
future.copy_on_write | False | 启用未来的 Copy-on-Write 默认行为 |
future.no_silent_downcasting | False | 禁止静默向下转型(如 float64 → int64) |
# 在 2.3 中启用 PyArrow 字符串推断
pd.set_option('future.infer_string', True)建议
生产环境中可通过
pd.set_option('future.copy_on_write', True)提前适配 pandas 3.x 行为。
相关笔记
- 20.3 获取与设置 - 如何使用这些选项
- 20.4 显示格式化 - display 选项的实际效果
- 性能优化与大数据-compute 与 future 选项的性能影响