10000 Merge pull request #14271 from anntzer/uniterable · matplotlib/matplotlib@d1a198e · GitHub
[go: up one dir, main page]

Skip to content

Commit d1a198e

Browse files
authored
Merge pull request #14271 from anntzer/uniterable
Replace some uses of np.iterable
2 parents 89105f1 + 9cb90e0 commit d1a198e

File tree

3 files changed

+19
-53
lines changed

3 files changed

+19
-53
lines changed

lib/matplotlib/dates.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,7 @@ def julian2num(j):
440440
float or sequence of floats
441441
Matplotlib date(s)
442442
"""
443-
if np.iterable(j):
444-
j = np.asarray(j)
445-
return j - JULIAN_OFFSET
443+
return np.subtract(j, JULIAN_OFFSET) # Handles both scalar & nonscalar j.
446444

447445

448446
def num2julian(n):
@@ -459,9 +457,7 @@ def num2julian(n):
459457
float or sequence of floats
460458
Julian date(s)
461459
"""
462-
if np.iterable(n):
463-
n = np.asarray(n)
464-
return n + JULIAN_OFFSET
460+
return np.add(n, JULIAN_OFFSET) # Handles both scalar & nonscalar j.
465461

466462

467463
def num2date(x, tz=None):

lib/matplotlib/testing/jpl_units/EpochConverter.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44

5+
from matplotlib import cbook
56
import matplotlib.units as units
67
import matplotlib.dates as date_ticker
78

@@ -95,28 +96,14 @@ def convert(value, unit, axis):
9596
# Delay-load due to circular dependencies.
9697
import matplotlib.testing.jpl_units as U
9798

98-
isNotEpoch = True
99-
isDuration = False
100-
101-
if np.iterable(value) and not isinstance(value, str):
102-
if len(value) == 0:
103-
return []
104-
else:
105-
return [EpochConverter.convert(x, unit, axis) for x in value]
106-
107-
if isinstance(value, U.Epoch):
108-
isNotEpoch = False
109-
elif isinstance(value, U.Duration):
110-
isDuration = True
111-
112-
if (isNotEpoch and not isDuration and
113-
units.ConversionInterface.is_numlike(value)):
99+
if not cbook.is_scalar_or_string(value):
100+
return [EpochConverter.convert(x, unit, axis) for x in value]
101+
if (units.ConversionInterface.is_numlike(value)
102+
and not isinstance(value, (U.Epoch, U.Duration))):
114103
return value
115-
116104
if unit is None:
117105
unit = EpochConverter.default_units(value, axis)
118-
119-
if isDuration:
106+
if isinstance(value, U.Duration):
120107
return EpochConverter.duration2float(value)
121108
else:
122109
return EpochConverter.epoch2float(value, unit)
@@ -131,10 +118,7 @@ def default_units(value, axis):
131118
= RETURN VALUE
132119
- Returns the default units to use for value.
133120
"""
134-
frame = None
135-
if np.iterable(value) and not isinstance(value, str):
136-
return EpochConverter.default_units(value[0], axis)
121+
if cbook.is_scalar_or_string(value):
122+
return value.frame()
137123
else:
138-
frame = value.frame()
139-
140-
return frame
124+
return EpochConverter.default_units(value[0], axis)

lib/matplotlib/testing/jpl_units/UnitDblConverter.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44

5+
from matplotlib import cbook
56
import matplotlib.units as units
67
import matplotlib.projections.polar as polar
78

@@ -83,35 +84,21 @@ def convert(value, unit, axis):
8384
# Delay-load due to circular dependencies.
8485
import matplotlib.testing.jpl_units as U
8586

86-
isNotUnitDbl = True
87-
88-
if np.iterable(value) and not isinstance(value, str):
89-
if len(value) == 0:
90-
return []
91-
else:
92-
return [UnitDblConverter.convert(x, unit, axis) for x in value]
93-
94-
# We need to check to see if the incoming value is actually a
95-
# UnitDbl and set a flag. If we get an empty list, then just
96-
# return an empty list.
97-
if isinstance(value, U.UnitDbl):
98-
isNotUnitDbl = False
99-
87+
if not cbook.is_scalar_or_string(value):
88+
return [UnitDblConverter.convert(x, unit, axis) for x in value]
10089
# If the incoming value behaves like a number, but is not a UnitDbl,
10190
# then just return it because we don't know how to convert it
10291
# (or it is already converted)
103-
if isNotUnitDbl and units.ConversionInterface.is_numlike(value):
92+
if (units.ConversionInterface.is_numlike(value)
93+
and not isinstance(value, U.UnitDbl)):
10494
return value
105-
10695
# If no units were specified, then get the default units to use.
10796
if unit is None:
10897
unit = UnitDblConverter.default_units(value, axis)
109-
11098
# Convert the incoming UnitDbl value/values to float/floats
11199
if isinstance(axis.axes, polar.PolarAxes) and value.type() == "angle":
112100
# Guarantee that units are radians for polar plots.
113101
return value.convert("rad")
114-
115102
return value.convert(unit)
116103

117104
@staticmethod
@@ -125,10 +112,9 @@ def default_units(value, axis):
125112
- Returns the default units to use for value.
126113
Return the default unit for value, or None.
127114
"""
128-
129115
# Determine the default units based on the user preferences set for
130116
# default units when printing a UnitDbl.
131-
if np.iterable(value) and not isinstance(value, str):
132-
return UnitDblConverter.default_units(value[0], axis)
133-
else:
117+
if cbook.is_scalar_or_string(value):
134118
return UnitDblConverter.defaults[value.type()]
119+
else:
120+
return UnitDblConverter.default_units(value[0], axis)

0 commit comments

Comments
 (0)
0