Skip to content

Commit c387fd8

Browse files
TomAugspurgerjreback
authored andcommitted
TST: Rework style test skip
Also redoes some of the changes that were inadvertently revereted in #12260. That PR had two commits, one of which was an older version of what was eventually merged in #12162. The stale commit incorrectly merged was a15248a. It should have been a3c38fe. For the most part the changes were just style, but there was a python2 import error, which our tests didn't fail on because I was trying to catch a jinja ImportError, and instead caught all ImportErrors.
1 parent c69037c commit c387fd8

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

pandas/core/style.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from contextlib import contextmanager
88
from uuid import uuid1
99
import copy
10-
from collections import defaultdict
11-
from collections.abc import MutableMapping
10+
from collections import defaultdict, MutableMapping
1211

1312
try:
1413
from jinja2 import Template
@@ -273,7 +272,7 @@ def _translate(self):
273272
caption=caption, table_attributes=self.table_attributes)
274273

275274
def format(self, formatter, subset=None):
276-
'''
275+
"""
277276
Format the text display value of cells.
278277
279278
.. versionadded:: 0.18.0
@@ -282,6 +281,8 @@ def format(self, formatter, subset=None):
282281
----------
283282
formatter: str, callable, or dict
284283
subset: IndexSlice
284+
An argument to ``DataFrame.loc`` that restricts which elements
285+
``formatter`` is applied to.
285286
286287
Returns
287288
-------
@@ -292,8 +293,9 @@ def format(self, formatter, subset=None):
292293
293294
``formatter`` is either an ``a`` or a dict ``{column name: a}`` where
294295
``a`` is one of
295-
- str: this will be wrapped in: ``a.format(x)``
296-
- callable: called with the value of an individual cell
296+
297+
- str: this will be wrapped in: ``a.format(x)``
298+
- callable: called with the value of an individual cell
297299
298300
The default display value for numeric values is the "general" (``g``)
299301
format with ``pd.options.display.precision`` precision.
@@ -305,7 +307,7 @@ def format(self, formatter, subset=None):
305307
>>> df.style.format("{:.2%}")
306308
>>> df['c'] = ['a', 'b', 'c', 'd']
307309
>>> df.style.format({'C': str.upper})
308-
'''
310+
"""
309311
if subset is None:
310312
row_locs = range(len(self.data))
311313
col_locs = range(len(self.data.columns))
@@ -853,11 +855,11 @@ def _highlight_extrema(data, color='yellow', max_=True):
853855

854856

855857
def _maybe_wrap_formatter(formatter):
856-
if not (callable(formatter) or com.is_string_like(formatter)):
858+
if com.is_string_like(formatter):
859+
return lambda x: formatter.format(x)
860+
elif callable(formatter):
861+
return formatter
862+
else:
857863
msg = "Expected a template string or callable, got {} instead".format(
858864
formatter)
859865
raise TypeError(msg)
860-
if not callable(formatter):
861-
return lambda x: formatter.format(x)
862-
else:
863-
return formatter

pandas/tests/test_style.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
if job_name == '27_slow_nnet_LOCALE':
1818
raise SkipTest("No jinja")
1919
try:
20-
from pandas.core.style import Styler
20+
# Do try except on just jinja, so the only reason
21+
# We skip is if jinja can't import, not something else
22+
import jinja2 # noqa
2123
except ImportError:
2224
raise SkipTest("No Jinja2")
25+
from pandas.core.style import Styler # noqa
2326

2427

2528
class TestStyler(TestCase):

0 commit comments

Comments
 (0)