Skip to content
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

Optimize copy_to_readonly_numpy_array for numeric pandas series/index objects #1149

Merged
merged 3 commits into from
Sep 2, 2018
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
11 changes: 10 additions & 1 deletion _plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,20 @@ def copy_to_readonly_numpy_array(v, dtype=None, force_numeric=False):
# u: unsigned int, i: signed int, f: float
numeric_kinds = ['u', 'i', 'f']

# Unwrap data types that have a `values` property that might be a numpy
# array. If this values property is a numeric numpy array then we
# can take the fast path below
if pd and isinstance(v, (pd.Series, pd.Index)):
v = v.values

if not isinstance(v, np.ndarray):
v_list = [to_scalar_or_list(e) for e in v]
new_v = np.array(v_list, order='C', dtype=dtype)
elif v.dtype.kind in numeric_kinds:
new_v = np.ascontiguousarray(v.astype(dtype))
if dtype:
new_v = np.ascontiguousarray(v.astype(dtype))
else:
new_v = np.ascontiguousarray(v)
else:
new_v = v.copy()

Expand Down
6 changes: 0 additions & 6 deletions _plotly_utils/tests/validators/test_color_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,6 @@ def test_description_aok(validator_aok):
assert 'A list or array of any of the above' in desc


def test_description_aok(validator_colorscale):
desc = validator_colorscale.description()
assert 'A number that will be interpreted as a color' in desc
assert 'A list or array of any of the above' not in desc


def test_description_aok_colorscale(validator_aok_colorscale):
desc = validator_aok_colorscale.description()
assert 'A number that will be interpreted as a color' in desc
Expand Down
8 changes: 6 additions & 2 deletions _plotly_utils/tests/validators/test_integer_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pytest import approx
from _plotly_utils.basevalidators import IntegerValidator
import numpy as np
import pandas as pd


# ### Fixtures ###
Expand Down Expand Up @@ -121,17 +122,20 @@ def test_acceptance_aok_list(val, validator_aok):
[([1, 0], (1, 0)),
((1, -1), (1, -1)),
(np.array([-1, 0, 5.0], dtype='int16'), [-1, 0, 5]),
(np.array([1, 0], dtype=np.int64), [1, 0])])
(np.array([1, 0], dtype=np.int64), [1, 0]),
(pd.Series([1, 0], dtype=np.int64), [1, 0]),
(pd.Index([1, 0], dtype=np.int64), [1, 0])])
def test_coercion_aok_list(val, expected, validator_aok):
v = validator_aok.validate_coerce(val)
if isinstance(val, np.ndarray):
if isinstance(val, (np.ndarray, pd.Series, pd.Index)):
assert v.dtype == np.int32
assert np.array_equal(validator_aok.present(v),
np.array(expected, dtype=np.int32))
else:
assert isinstance(v, list)
assert validator_aok.present(v) == expected


# ### Rejection ###
#
@pytest.mark.parametrize('val',
Expand Down
9 changes: 5 additions & 4 deletions _plotly_utils/tests/validators/test_number_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from _plotly_utils.basevalidators import NumberValidator
import numpy as np

import pandas as pd

# Fixtures
# --------
Expand Down Expand Up @@ -118,12 +118,13 @@ def test_acceptance_aok_list(val, validator_aok):
# Coerced to general consistent numeric type
@pytest.mark.parametrize('val,expected',
[([1.0, 0], (1.0, 0)),
(np.array([1, -1]), np.array([1.0, -1.0])),
(np.array([1, -1]), np.array([1, -1])),
(pd.Series([1, -1]), np.array([1, -1])),
(pd.Index([1, -1]), np.array([1, -1])),
((-0.1234, 0, -1), (-0.1234, 0.0, -1.0))])
def test_coercion_aok_list(val, expected, validator_aok):
v = validator_aok.validate_coerce(val)
if isinstance(val, np.ndarray):
assert v.dtype == 'float'
if isinstance(val, (np.ndarray, pd.Series, pd.Index)):
assert np.array_equal(v, expected)
else:
assert isinstance(v, list)
Expand Down