8000 Improve Pandas conversion · matplotlib/matplotlib@64a301e · GitHub
[go: up one dir, main page]

Skip to content

Commit 64a301e

Browse files
Oscar Gustafssonoscargus
Oscar Gustafsson
authored andcommitted
Improve Pandas conversion
1 parent 2e95730 commit 64a301e

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7907,8 +7907,8 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
79077907
"""
79087908

79097909
def _kde_method(X, coords):
7910-
if hasattr(X, 'values'): # support pandas.Series
7911-
X = X.values
7910+
# Unpack in case of Pandas object
7911+
X = cbook._unpack_pandas(X)
79127912
# fallback gracefully if the vector contains only one value
79137913
if np.all(X[0] == X):
79147914
return (X[0] == coords).astype(float)

lib/matplotlib/cbook/__init__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,15 +1377,8 @@ def _reshape_2D(X, name):
13771377
*name* is used to generate the error message for invalid inputs.
13781378
"""
13791379

1380-
# unpack if we have a values or to_numpy method.
1381-
try:
1382-
X = X.to_numpy()
1383-
except AttributeError:
1384-
try:
1385-
if isinstance(X.values, np.ndarray):
1386-
X = X.values
1387-
except AttributeError:
1388-
pass
1380+
# Unpack in case of Pandas object
1381+
X = _unpack_pandas(X)
13891382

13901383
# Iterate over columns for ndarrays.
13911384
if isinstance(X, np.ndarray):
@@ -2276,3 +2269,17 @@ def _picklable_class_constructor(mixin_class, fmt, attr_name, base_class):
22762269
factory = _make_class_factory(mixin_class, fmt, attr_name)
22772270
cls = factory(base_class)
22782271
return cls.__new__(cls)
2272+
2273+
2274+
def _unpack_pandas(x):
2275+
"""Internal helper to extract data from Pandas object."""
2276+
if hasattr(x, 'to_numpy'):
2277+
# Assume that any function to_numpy() do actually return a numpy array
2278+
return x.to_numpy()
2279+
if hasattr(x, 'values'):
2280+
xtmp = x.values
2281+
# For example a dict has a 'values' attribute, but it is not a property
2282+
# so in this case we do not want to return a function
2283+
if isinstance(xtmp, np.ndarray):
2284+
return xtmp
2285+
return x

lib/matplotlib/dates.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,8 @@ def date2num(d):
437437
The Gregorian calendar is assumed; this is not universal practice.
438438
For details see the module docstring.
439439
"""
440-
if hasattr(d, "values"):
441-
# this unpacks pandas series or dataframes...
442-
d = d.values
440+
# Unpack in case of Pandas object
441+
d = cbook._unpack_pandas(d)
443442

444443
# make an iterable, but save state to unpack later:
445444
iterable = np.iterable(d)

lib/matplotlib/units.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ class Registry(dict):
180180

181181
def get_converter(self, x):
182182
"""Get the converter interface instance for *x*, or None."""
183-
if hasattr(x, "values"):
184-
x = x.values # Unpack pandas Series and DataFrames.
183+
# Unpack in case of Pandas object
184+
x = cbook._unpack_pandas(x)
185+
185186
if isinstance(x, np.ndarray):
186187
# In case x in a masked array, access the underlying data (only its
187188
# type matters). If x is a regular ndarray, getdata() just returns

0 commit comments

Comments
 (0)
0