Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit64a301e

Browse files
Oscar Gustafssonoscargus
Oscar Gustafsson
authored andcommitted
Improve Pandas conversion
1 parent2e95730 commit64a301e

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-
ifhasattr(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
ifnp.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-
exceptAttributeError:
1384-
try:
1385-
ifisinstance(X.values,np.ndarray):
1386-
X=X.values
1387-
exceptAttributeError:
1388-
pass
1380+
# Unpack in case of Pandas object
1381+
X=_unpack_pandas(X)
13891382

13901383
# Iterate over columns for ndarrays.
13911384
ifisinstance(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
returncls.__new__(cls)
2272+
2273+
2274+
def_unpack_pandas(x):
2275+
"""Internal helper to extract data from Pandas object."""
2276+
ifhasattr(x,'to_numpy'):
2277+
# Assume that any function to_numpy() do actually return a numpy array
2278+
returnx.to_numpy()
2279+
ifhasattr(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+
ifisinstance(xtmp,np.ndarray):
2284+
returnxtmp
2285+
returnx

‎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-
ifhasattr(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
defget_converter(self,x):
182182
"""Get the converter interface instance for *x*, or None."""
183-
ifhasattr(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
ifisinstance(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)

[8]ページ先頭

©2009-2025 Movatter.jp