Skip to content

PEP: pandas/core round 6 (config*, convert, datetools, strings, style) #12095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions pandas/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
import pandas.compat as compat

DeprecatedOption = namedtuple('DeprecatedOption', 'key msg rkey removal_ver')
RegisteredOption = namedtuple(
'RegisteredOption', 'key defval doc validator cb')
RegisteredOption = namedtuple('RegisteredOption',
'key defval doc validator cb')

_deprecated_options = {} # holds deprecated option metdata
_registered_options = {} # holds registered option metdata
Expand All @@ -67,14 +67,14 @@


class OptionError(AttributeError, KeyError):

"""Exception for pandas.options, backwards compatible with KeyError
checks"""

checks
"""

#
# User API


def _get_single_key(pat, silent):
keys = _select_options(pat)
if len(keys) == 0:
Expand Down Expand Up @@ -106,14 +106,14 @@ def _set_option(*args, **kwargs):
nargs = len(args)
if not nargs or nargs % 2 != 0:
raise ValueError("Must provide an even number of non-keyword "
"arguments")
"arguments")

# default to false
silent = kwargs.pop('silent', False)

if kwargs:
raise TypeError('_set_option() got an unexpected keyword '
'argument "{0}"'.format(list(kwargs.keys())[0]))
'argument "{0}"'.format(list(kwargs.keys())[0]))

for k, v in zip(args[::2], args[1::2]):
key = _get_single_key(k, silent)
Expand All @@ -129,6 +129,7 @@ def _set_option(*args, **kwargs):
if o.cb:
o.cb(key)


def _describe_option(pat='', _print_desc=True):

keys = _select_options(pat)
Expand Down Expand Up @@ -168,9 +169,7 @@ def get_default_val(pat):


class DictWrapper(object):

""" provide attribute-style access to a nested dict
"""
""" provide attribute-style access to a nested dict"""

def __init__(self, d, prefix=""):
object.__setattr__(self, "d", d)
Expand Down Expand Up @@ -202,7 +201,6 @@ def __getattr__(self, key):
def __dir__(self):
return list(self.d.keys())


# For user convenience, we'd like to have the available options described
# in the docstring. For dev convenience we'd like to generate the docstrings
# dynamically instead of maintaining them by hand. To this, we use the
Expand All @@ -213,7 +211,6 @@ def __dir__(self):


class CallableDynamicDoc(object):

def __init__(self, func, doc_tmpl):
self.__doc_tmpl__ = doc_tmpl
self.__func__ = func
Expand All @@ -228,6 +225,7 @@ def __doc__(self):
return self.__doc_tmpl__.format(opts_desc=opts_desc,
opts_list=opts_list)


_get_option_tmpl = """
get_option(pat)

Expand Down Expand Up @@ -384,10 +382,8 @@ class option_context(object):

def __init__(self, *args):
if not (len(args) % 2 == 0 and len(args) >= 2):
raise ValueError(
'Need to invoke as'
'option_context(pat, val, [(pat, val), ...)).'
)
raise ValueError('Need to invoke as'
'option_context(pat, val, [(pat, val), ...)).')

self.ops = list(zip(args[::2], args[1::2]))

Expand Down Expand Up @@ -462,8 +458,8 @@ def register_option(key, defval, doc='', validator=None, cb=None):
cursor = cursor[p]

if not isinstance(cursor, dict):
raise OptionError("Path prefix to option '%s' is already an option"
% '.'.join(path[:-1]))
raise OptionError("Path prefix to option '%s' is already an option" %
'.'.join(path[:-1]))

cursor[path[-1]] = defval # initialize

Expand Down Expand Up @@ -520,10 +516,10 @@ def deprecate_option(key, msg=None, rkey=None, removal_ver=None):

_deprecated_options[key] = DeprecatedOption(key, msg, rkey, removal_ver)


#
# functions internal to the module


def _select_options(pat):
"""returns a list of keys matching `pat`

Expand Down Expand Up @@ -681,7 +677,6 @@ def pp(name, ks):
else:
return s


#
# helpers

Expand Down Expand Up @@ -717,7 +712,6 @@ def config_prefix(prefix):
global register_option, get_option, set_option, reset_option

def wrap(func):

def inner(key, *args, **kwds):
pkey = '%s.%s' % (prefix, key)
return func(pkey, *args, **kwds)
Expand All @@ -735,10 +729,10 @@ def inner(key, *args, **kwds):
get_option = _get_option
register_option = _register_option


# These factories and methods are handy for use as the validator
# arg in register_option


def is_type_factory(_type):
"""

Expand Down Expand Up @@ -790,10 +784,10 @@ def inner(x):
def is_one_of_factory(legal_values):
def inner(x):
from pandas.core.common import pprint_thing as pp
if not x in legal_values:
if x not in legal_values:
pp_values = lmap(pp, legal_values)
raise ValueError("Value must be one of %s"
% pp("|".join(pp_values)))
raise ValueError("Value must be one of %s" %
pp("|".join(pp_values)))

return inner

Expand Down
56 changes: 28 additions & 28 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
"""

import pandas.core.config as cf
from pandas.core.config import (is_int, is_bool, is_text, is_float,
is_instance_factory, is_one_of_factory,
get_default_val)
from pandas.core.config import (is_int, is_bool, is_text, is_instance_factory,
is_one_of_factory, get_default_val)
from pandas.core.format import detect_console_encoding


#
# options from the "display" namespace

Expand Down Expand Up @@ -61,8 +59,8 @@

pc_max_categories_doc = """
: int
This sets the maximum number of categories pandas should output when printing
out a `Categorical` or a Series of dtype "category".
This sets the maximum number of categories pandas should output when
printing out a `Categorical` or a Series of dtype "category".
"""

pc_max_info_cols_doc = """
Expand Down Expand Up @@ -146,9 +144,11 @@

pc_east_asian_width_doc = """
: boolean
Whether to use the Unicode East Asian Width to calculate the display text width
Whether to use the Unicode East Asian Width to calculate the display text
width.
Enabling this may affect to the performance (default: False)
"""

pc_ambiguous_as_wide_doc = """
: boolean
Whether to handle Unicode characters belong to Ambiguous as Wide (width=2)
Expand Down Expand Up @@ -197,7 +197,8 @@
: int or None
df.info() will usually show null-counts for each column.
For large frames this can be quite slow. max_info_rows and max_info_cols
limit this null check only to frames with smaller dimensions then specified.
limit this null check only to frames with smaller dimensions than
specified.
"""

pc_large_repr_doc = """
Expand All @@ -222,15 +223,16 @@

pc_latex_escape = """
: bool
This specifies if the to_latex method of a Dataframe uses escapes special
This specifies if the to_latex method of a Dataframe uses escapes special
characters.
method. Valid values: False,True
method. Valid values: False,True
"""

pc_latex_longtable = """
:bool
This specifies if the to_latex method of a Dataframe uses the longtable format.
method. Valid values: False,True
This specifies if the to_latex method of a Dataframe uses the longtable
format.
method. Valid values: False,True
"""

style_backup = dict()
Expand All @@ -244,7 +246,7 @@ def mpl_style_cb(key):
val = cf.get_option(key)

if 'matplotlib' not in sys.modules.keys():
if not(val): # starting up, we get reset to None
if not val: # starting up, we get reset to None
return val
raise Exception("matplotlib has not been imported. aborting")

Expand All @@ -267,7 +269,8 @@ def mpl_style_cb(key):
validator=is_instance_factory((int, type(None))))
cf.register_option('max_rows', 60, pc_max_rows_doc,
validator=is_instance_factory([type(None), int]))
cf.register_option('max_categories', 8, pc_max_categories_doc, validator=is_int)
cf.register_option('max_categories', 8, pc_max_categories_doc,
validator=is_int)
cf.register_option('max_colwidth', 50, max_colwidth_doc, validator=is_int)
cf.register_option('max_columns', 20, pc_max_cols_doc,
validator=is_instance_factory([type(None), int]))
Expand Down Expand Up @@ -305,28 +308,29 @@ def mpl_style_cb(key):
cf.register_option('line_width', get_default_val('display.width'),
pc_line_width_doc)
cf.register_option('memory_usage', True, pc_memory_usage_doc,
validator=is_one_of_factory([None, True, False, 'deep']))
validator=is_one_of_factory([None, True,
False, 'deep']))
cf.register_option('unicode.east_asian_width', False,
pc_east_asian_width_doc, validator=is_bool)
cf.register_option('unicode.ambiguous_as_wide', False,
pc_east_asian_width_doc, validator=is_bool)
cf.register_option('latex.escape',True, pc_latex_escape,
validator=is_bool)
cf.register_option('latex.longtable',False,pc_latex_longtable,
validator=is_bool)
cf.register_option('latex.escape', True, pc_latex_escape,
validator=is_bool)
cf.register_option('latex.longtable', False, pc_latex_longtable,
validator=is_bool)

cf.deprecate_option('display.line_width',
msg=pc_line_width_deprecation_warning,
rkey='display.width')

cf.deprecate_option('display.height',
msg=pc_height_deprecation_warning,
cf.deprecate_option('display.height', msg=pc_height_deprecation_warning,
rkey='display.max_rows')

tc_sim_interactive_doc = """
: boolean
Whether to simulate interactive mode for purposes of testing
"""

with cf.config_prefix('mode'):
cf.register_option('sim_interactive', False, tc_sim_interactive_doc)

Expand All @@ -349,7 +353,6 @@ def use_inf_as_null_cb(key):
cf.register_option('use_inf_as_null', False, use_inf_as_null_doc,
cb=use_inf_as_null_cb)


# user warnings
chained_assignment = """
: string
Expand All @@ -361,7 +364,6 @@ def use_inf_as_null_cb(key):
cf.register_option('chained_assignment', 'warn', chained_assignment,
validator=is_one_of_factory([None, 'warn', 'raise']))


# Set up the io.excel specific configuration.
writer_engine_doc = """
: string
Expand All @@ -371,8 +373,7 @@ def use_inf_as_null_cb(key):

with cf.config_prefix('io.excel'):
# going forward, will be additional writers
for ext, options in [('xls', ['xlwt']),
('xlsm', ['openpyxl'])]:
for ext, options in [('xls', ['xlwt']), ('xlsm', ['openpyxl'])]:
default = options.pop(0)
if options:
options = " " + ", ".join(options)
Expand All @@ -384,14 +385,13 @@ def use_inf_as_null_cb(key):

def _register_xlsx(engine, other):
cf.register_option('xlsx.writer', engine,
writer_engine_doc.format(ext='xlsx',
default=engine,
writer_engine_doc.format(ext='xlsx', default=engine,
others=", '%s'" % other),
validator=str)

try:
# better memory footprint
import xlsxwriter
import xlsxwriter # noqa
_register_xlsx('xlsxwriter', 'openpyxl')
except ImportError:
# fallback
Expand Down
Loading