Skip to content

shims for categorical support for numpy < 1.8 #8591

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

Merged
merged 3 commits into from
May 12, 2017
Merged
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
30 changes: 25 additions & 5 deletions lib/matplotlib/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import six

import numpy as np

import matplotlib.cbook as cbook
import matplotlib.units as units
import matplotlib.ticker as ticker

# np 1.6/1.7 support
from distutils.version import LooseVersion
import collections


if LooseVersion(np.__version__) >= LooseVersion('1.8.0'):
def shim_array(data):
return np.array(data, dtype=np.unicode)
else:
def shim_array(data):
if (isinstance(data, six.string_types) or
not isinstance(data, collections.Iterable)):
data = [data]
try:
data = [str(d) for d in data]
except UnicodeEncodeError:
# this yields gibberish but unicode text doesn't
# render under numpy1.6 anyway
data = [d.encode('utf-8', 'ignore').decode('utf-8')
for d in data]
return np.array(data, dtype=np.unicode)


class StrCategoryConverter(units.ConversionInterface):
@staticmethod
Expand All @@ -25,7 +45,8 @@ def convert(value, unit, axis):
if isinstance(value, six.string_types):
return vmap[value]

vals = np.array(value, dtype=np.unicode)
vals = shim_array(value)

for lab, loc in vmap.items():
vals[vals == lab] = loc

Expand Down Expand Up @@ -81,8 +102,7 @@ def update(self, new_data):
self._set_seq_locs(new_data, value)

def _set_seq_locs(self, data, value):
strdata = np.array(data, dtype=np.unicode)
# np.unique makes dateframes work
strdata = shim_array(data)
new_s = [d for d in np.unique(strdata) if d not in self.seq]
for ns in new_s:
self.seq.append(ns)
Expand Down
10 changes: 0 additions & 10 deletions lib/matplotlib/tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from distutils.version import LooseVersion

import pytest
import numpy as np

Expand All @@ -14,11 +12,6 @@
import unittest


needs_new_numpy = pytest.mark.xfail(
LooseVersion(np.__version__) < LooseVersion('1.8.0'),
reason='NumPy < 1.8.0 is broken.')


class TestUnitData(object):
testdata = [("hello world", ["hello world"], [0]),
("Здравствуйте мир", ["Здравствуйте мир"], [0]),
Expand All @@ -28,14 +21,12 @@ class TestUnitData(object):

ids = ["single", "unicode", "mixed"]

@needs_new_numpy
@pytest.mark.parametrize("data, seq, locs", testdata, ids=ids)
def test_unit(self, data, seq, locs):
act = cat.UnitData(data)
assert act.seq == seq
assert act.locs == locs

@needs_new_numpy
def test_update_map(self):
data = ['a', 'd']
oseq = ['a', 'd']
Expand Down Expand Up @@ -87,7 +78,6 @@ class TestStrCategoryConverter(object):
def mock_axis(self, request):
self.cc = cat.StrCategoryConverter()

@needs_new_numpy
@pytest.mark.parametrize("data, unitmap, exp", testdata, ids=ids)
def test_convert(self, data, unitmap, exp):
MUD = MockUnitData(unitmap)
Expand Down