66_Pandas How to check and change option settings

66_How to check and change option settings in Pandas

Using pandas, you can customize behavior and display by changing option settings. Explains how to check and change various setting values.

The following content is explained here.

  • Access properties to inspect and change settings: options
  • Display configuration information in list form: describe_option()
  • Use functions to check and change setting values: get_option(), set_option()
  • Check and change multiple setting values at once
  • Restore default settings: reset_option()
  • Temporarily change settings in a with block: option_context()
import pandas as pd
import print

print(pd.__version__)
#0.23.0

Access properties to examine and change settings: options

Each setting value can be accessed, inspected, and changed using properties under pd.options.

print(pd.options.display.max_rows)
#60

pd.options.display.max_rows = 100

print(pd.options.display.max_rows)
#100

In Jupyter Notebook or an editor that supports completion, you can use the TAB key to display completion suggestions, which is very convenient. You can also use the built-in function dir() to check what items are directly below.

print(dir(pd.options))
# ['compute', 'display', 'html', 'io', 'mode', 'plotting']

pprint.pprint(dir(pd.options.display))
# ['chop_threshold',
# 'colheader_justify',
# 'column_space',
# 'date_dayfirst',
# 'date_yearfirst',
# 'encoding',
# 'expand_frame_repr',
# 'float_format',
# 'html',
# 'large_repr',
# 'latex',
# 'max_categories',
# 'max_columns',
# 'max_colwidth',
# 'max_info_columns',
# 'max_info_rows',
# 'max_rows',
# 'max_seq_items',
# 'memory_usage',
# 'multi_sparse',
# 'notebook_repr_html',
# 'pprint_nest_depth',
# 'precision',
# 'show_dimensions',
# 'unicode',
# 'width']

Display configuration information in list form: describe_option()

Use the pd.describe_option() function to display the description, default value, and current value of each setting item.

If this parameter is omitted, information about all setting items will be displayed. Due to the large amount, the output is omitted here.

pd.describe_option()

Specify the regular expression pattern string as argument. Information on setting items matching the pattern will be displayed. If you specify a simple string without using special characters in the regular expression, the setting items containing the string will be displayed.

pd.describe_option('compute')
# compute.use_bottleneck : bool
# Use the bottleneck library to accelerate if it is installed,
# the default is True
# Valid values: False,True
# [default: True] [currently: True]
# compute.use_numexpr : bool
# Use the numexpr library to accelerate computation if it is installed,
# the default is True
# Valid values: False,True
# [default: True] [currently: True]

pd.describe_option('max_col')
# display.max_columns : int
# If max_cols is exceeded, switch to truncate view. Depending on
# `large_repr`, objects are either centrally truncated or printed as
# a summary view. 'None' value means unlimited.
# In case python/IPython is running in a terminal and `large_repr`
# equals 'truncate' this can be set to 0 and pandas will auto-detect
# the width of the terminal and print a truncated object which fits
# the screen width. The IPython notebook, IPython qtconsole, or IDLE
# do not run in a terminal and hence it is not possible to do
# correct auto-detection.
# [default: 20] [currently: 20]
# display.max_colwidth : int
# The maximum width in characters of a column in the repr of
# a pandas data structure. When the column overflows, a "..."
# placeholder is embedded in the output.
# [default: 50] [currently: 50]

[default: xxx] [currently: xxx] at the end of each setting item is the default value and current value. You can also use special characters in regular expressions.

pd.describe_option('max.*col')
# display.max_columns : int
# If max_cols is exceeded, switch to truncate view. Depending on
# `large_repr`, objects are either centrally truncated or printed as
# a summary view. 'None' value means unlimited.
# In case python/IPython is running in a terminal and `large_repr`
# equals 'truncate' this can be set to 0 and pandas will auto-detect
# the width of the terminal and print a truncated object which fits
# the screen width. The IPython notebook, IPython qtconsole, or IDLE
# do not run in a terminal and hence it is not possible to do
# correct auto-detection.
# [default: 20] [currently: 20]
# display.max_colwidth : int
# The maximum width in characters of a column in the repr of
# a pandas data structure. When the column overflows, a "..."
# placeholder is embedded in the output.
# [default: 50] [currently: 50]
# display.max_info_columns : int
# max_info_columns is used in DataFrame.info method to decide if
# per column information will be printed.
# [default: 100] [currently: 100]

Use functions to check and change setting values: get_option(), set_option()

In addition to using the properties described above to access setting values, you can also use functions to inspect and change them.

Specify the regular expression pattern string as argument. Returns the current value of the setting that matches the pattern.

print(pd.get_option('display.max_rows'))
#100

Specify the regular expression pattern string for the first argument and the value to set for the second argument. The value of the setting item matching the pattern is changed to the specified value.

pd.set_option('display.max_rows', 60)

Since it is specified using a regular expression pattern, the full item name does not need to be specified, but if multiple items match, an OptionError will occur.

print(pd.get_option('max_r'))
#60

pd.set_option('max_r', 100)

# pd.get_option('max')
# OptionError: 'Pattern matched multiple keys'

# pd.set_option('max', 60)
# OptionError: 'Pattern matched multiple keys'

If you specify anything other than the full project name, there is a chance that errors will occur when matching newly added projects in the future, so it is best to use the full project name for code that will be used long-term.

Check and change multiple settings at once

As mentioned above, methods using attributes and the functions get_option() and set_option() can only access one setting value at a time, and there is no official way to check or change multiple setting values at once.

l = ['display.max_rows', 'display.max_columns', 'display.max_colwidth']

print([pd.get_option(i) for i in l])
# [100, 20, 50]

print({<!-- -->i: pd.get_option(i) for i in l})
# {'display.max_rows': 100, 'display.max_columns': 20, 'display.max_colwidth': 50}

Change and check multiple setting values at once from a dictionary of project names and required values. Use the dictionary methods items() and keys().

d = {<!-- -->'display.max_rows': 80,
     'display.max_columns': 80,
     'display.max_colwidth': 80}

[pd.set_option(k, v) for k, v in d.items()]

print({<!-- -->i: pd.get_option(i) for i in d.keys()})
# {'display.max_rows': 80, 'display.max_columns': 80, 'display.max_colwidth': 80}

Restore default settings: reset_option()

The function that returns the default settings is reset_option().

Specify the regular expression pattern string as argument. The values of setting items matching the pattern are reset to their default values.

print(pd.options.display.max_rows)
#80

pd.reset_option('display.max_rows')

print(pd.options.display.max_rows)
#60

Just match multiple items. All matching items will be reset.

print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
#80
#80

pd.reset_option('max_col')

print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
#20
#50

You can also use the regular expression special character matching the beginning ^ to reset all items on the display.

pd.options.display.max_rows = 100
pd.options.display.max_columns = 100
pd.options.display.max_colwidth = 100

pd.reset_option('^display', silent=True)

print(pd.options.display.max_rows)
print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
#60
#20
#50

If the parameter is “all”, all items will be reset. Since all projects are accessed, a FutureWarning may occur depending on the version.

pd.reset_option('all')
# html.border has been deprecated, use display.html.border instead
# (currently both are identical)
# : boolean
# use_inf_as_null had been deprecated and will be removed in a future
# version. Use `use_inf_as_na` instead.
# /usr/local/lib/python3.6/site-packages/pandas/core/config.py:619: FutureWarning: html.border has been deprecated, use display.html.border instead
# (currently both are identical)
# warnings.warn(d.msg, FutureWarning)
# /usr/local/lib/python3.6/site-packages/pandas/core/config.py:619: FutureWarning:
# : boolean
# use_inf_as_null had been deprecated and will be removed in a future
# version. Use `use_inf_as_na` instead.
# warnings.warn(d.msg, FutureWarning)

If you do not want to output FutureWarning, please also specify the silent=True parameter.

pd.reset_option('all', silent=True)

Temporarily change settings in a with block: option_context()

If you use pd.option_context() inside a with block, the settings will only change within that block.

Specify the regular expression pattern string for the first argument and the value to set for the second argument. The value of the setting item matching the pattern is changed to the specified value. Settings are only changed within the with block and are returned to the original value when the block is exited.

with pd.option_context('display.max_rows', 100):
    print(pd.options.display.max_rows)
#100

print(pd.options.display.max_rows)
#60

Instead of returning the default value, it returns the value before entering the with block.

pd.options.display.max_rows = 80

with pd.option_context('display.max_rows', 100):
    print(pd.options.display.max_rows)
#100

print(pd.options.display.max_rows)
#80

Multiple setting items can be changed by repeatedly specifying the regular expression pattern string (pat, val, pat, val,…) and the setting value as parameters.

with pd.option_context('display.max_rows', 100, 'display.max_columns', 100):
    print(pd.options.display.max_rows)
    print(pd.options.display.max_columns)
#100
#100

print(pd.options.display.max_rows)
print(pd.options.display.max_columns)
#80
#20

Using pd.option_context() outside a with block does not change the value.

pd.option_context('display.max_rows', 100)

print(pd.options.display.max_rows)
#80