Skip to content

Commit 3d4fd0d

Browse files
isaac-chungMarcoGorelli
authored and
MarcoGorelli
committed
STYLE: Fix redefined-outer-name linting issue (pandas-dev#49687)
* fixed linting issue in _matplotlib * run precommit * fix attribute error * fix references in test file
1 parent 6b43767 commit 3d4fd0d

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

pandas/plotting/_matplotlib/converter.py

+30-32
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
)
1818

1919
from dateutil.relativedelta import relativedelta
20-
from matplotlib import (
21-
dates,
22-
units,
23-
)
20+
import matplotlib.dates as mdates
2421
from matplotlib.ticker import (
2522
AutoLocator,
2623
Formatter,
2724
Locator,
2825
)
2926
from matplotlib.transforms import nonsingular
27+
import matplotlib.units as munits
3028
import numpy as np
3129

3230
from pandas._libs import lib
@@ -126,25 +124,25 @@ def register() -> None:
126124
pairs = get_pairs()
127125
for type_, cls in pairs:
128126
# Cache previous converter if present
129-
if type_ in units.registry and not isinstance(units.registry[type_], cls):
130-
previous = units.registry[type_]
127+
if type_ in munits.registry and not isinstance(munits.registry[type_], cls):
128+
previous = munits.registry[type_]
131129
_mpl_units[type_] = previous
132130
# Replace with pandas converter
133-
units.registry[type_] = cls()
131+
munits.registry[type_] = cls()
134132

135133

136134
def deregister() -> None:
137135
# Renamed in pandas.plotting.__init__
138136
for type_, cls in get_pairs():
139137
# We use type to catch our classes directly, no inheritance
140-
if type(units.registry.get(type_)) is cls:
141-
units.registry.pop(type_)
138+
if type(munits.registry.get(type_)) is cls:
139+
munits.registry.pop(type_)
142140

143141
# restore the old keys
144142
for unit, formatter in _mpl_units.items():
145143
if type(formatter) not in {DatetimeConverter, PeriodConverter, TimeConverter}:
146144
# make it idempotent by excluding ours.
147-
units.registry[unit] = formatter
145+
munits.registry[unit] = formatter
148146

149147

150148
def _to_ordinalf(tm: pydt.time) -> float:
@@ -161,7 +159,7 @@ def time2num(d):
161159
return d
162160

163161

164-
class TimeConverter(units.ConversionInterface):
162+
class TimeConverter(munits.ConversionInterface):
165163
@staticmethod
166164
def convert(value, unit, axis):
167165
valid_types = (str, pydt.time)
@@ -174,13 +172,13 @@ def convert(value, unit, axis):
174172
return value
175173

176174
@staticmethod
177-
def axisinfo(unit, axis) -> units.AxisInfo | None:
175+
def axisinfo(unit, axis) -> munits.AxisInfo | None:
178176
if unit != "time":
179177
return None
180178

181179
majloc = AutoLocator()
182180
majfmt = TimeFormatter(majloc)
183-
return units.AxisInfo(majloc=majloc, majfmt=majfmt, label="time")
181+
return munits.AxisInfo(majloc=majloc, majfmt=majfmt, label="time")
184182

185183
@staticmethod
186184
def default_units(x, axis) -> str:
@@ -231,7 +229,7 @@ def __call__(self, x, pos: int = 0) -> str:
231229
# Period Conversion
232230

233231

234-
class PeriodConverter(dates.DateConverter):
232+
class PeriodConverter(mdates.DateConverter):
235233
@staticmethod
236234
def convert(values, units, axis):
237235
if is_nested_list_like(values):
@@ -277,7 +275,7 @@ def get_datevalue(date, freq):
277275

278276

279277
# Datetime Conversion
280-
class DatetimeConverter(dates.DateConverter):
278+
class DatetimeConverter(mdates.DateConverter):
281279
@staticmethod
282280
def convert(values, unit, axis):
283281
# values might be a 1-d array, or a list-like of arrays.
@@ -291,12 +289,12 @@ def convert(values, unit, axis):
291289
def _convert_1d(values, unit, axis):
292290
def try_parse(values):
293291
try:
294-
return dates.date2num(tools.to_datetime(values))
292+
return mdates.date2num(tools.to_datetime(values))
295293
except Exception:
296294
return values
297295

298296
if isinstance(values, (datetime, pydt.date, np.datetime64, pydt.time)):
299-
return dates.date2num(values)
297+
return mdates.date2num(values)
300298
elif is_integer(values) or is_float(values):
301299
return values
302300
elif isinstance(values, str):
@@ -319,12 +317,12 @@ def try_parse(values):
319317
except Exception:
320318
pass
321319

322-
values = dates.date2num(values)
320+
values = mdates.date2num(values)
323321

324322
return values
325323

326324
@staticmethod
327-
def axisinfo(unit: tzinfo | None, axis) -> units.AxisInfo:
325+
def axisinfo(unit: tzinfo | None, axis) -> munits.AxisInfo:
328326
"""
329327
Return the :class:`~matplotlib.units.AxisInfo` for *unit*.
330328
@@ -338,17 +336,17 @@ def axisinfo(unit: tzinfo | None, axis) -> units.AxisInfo:
338336
datemin = pydt.date(2000, 1, 1)
339337
datemax = pydt.date(2010, 1, 1)
340338

341-
return units.AxisInfo(
339+
return munits.AxisInfo(
342340
majloc=majloc, majfmt=majfmt, label="", default_limits=(datemin, datemax)
343341
)
344342

345343

346-
class PandasAutoDateFormatter(dates.AutoDateFormatter):
344+
class PandasAutoDateFormatter(mdates.AutoDateFormatter):
347345
def __init__(self, locator, tz=None, defaultfmt: str = "%Y-%m-%d") -> None:
348-
dates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)
346+
mdates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)
349347

350348

351-
class PandasAutoDateLocator(dates.AutoDateLocator):
349+
class PandasAutoDateLocator(mdates.AutoDateLocator):
352350
def get_locator(self, dmin, dmax):
353351
"""Pick the best locator based on a distance."""
354352
delta = relativedelta(dmax, dmin)
@@ -366,26 +364,26 @@ def get_locator(self, dmin, dmax):
366364
locator.axis.set_data_interval(*self.axis.get_data_interval())
367365
return locator
368366

369-
return dates.AutoDateLocator.get_locator(self, dmin, dmax)
367+
return mdates.AutoDateLocator.get_locator(self, dmin, dmax)
370368

371369
def _get_unit(self):
372370
return MilliSecondLocator.get_unit_generic(self._freq)
373371

374372

375-
class MilliSecondLocator(dates.DateLocator):
373+
class MilliSecondLocator(mdates.DateLocator):
376374

377375
UNIT = 1.0 / (24 * 3600 * 1000)
378376

379377
def __init__(self, tz) -> None:
380-
dates.DateLocator.__init__(self, tz)
378+
mdates.DateLocator.__init__(self, tz)
381379
self._interval = 1.0
382380

383381
def _get_unit(self):
384382
return self.get_unit_generic(-1)
385383

386384
@staticmethod
387385
def get_unit_generic(freq):
388-
unit = dates.RRuleLocator.get_unit_generic(freq)
386+
unit = mdates.RRuleLocator.get_unit_generic(freq)
389387
if unit < 0:
390388
return MilliSecondLocator.UNIT
391389
return unit
@@ -398,7 +396,7 @@ def __call__(self):
398396
return []
399397

400398
# We need to cap at the endpoints of valid datetime
401-
nmax, nmin = dates.date2num((dmax, dmin))
399+
nmax, nmin = mdates.date2num((dmax, dmin))
402400

403401
num = (nmax - nmin) * 86400 * 1000
404402
max_millis_ticks = 6
@@ -427,12 +425,12 @@ def __call__(self):
427425

428426
try:
429427
if len(all_dates) > 0:
430-
locs = self.raise_if_exceeds(dates.date2num(all_dates))
428+
locs = self.raise_if_exceeds(mdates.date2num(all_dates))
431429
return locs
432430
except Exception: # pragma: no cover
433431
pass
434432

435-
lims = dates.date2num([dmin, dmax])
433+
lims = mdates.date2num([dmin, dmax])
436434
return lims
437435

438436
def _get_interval(self):
@@ -445,8 +443,8 @@ def autoscale(self):
445443
# We need to cap at the endpoints of valid datetime
446444
dmin, dmax = self.datalim_to_dt()
447445

448-
vmin = dates.date2num(dmin)
449-
vmax = dates.date2num(dmax)
446+
vmin = mdates.date2num(dmin)
447+
vmax = mdates.date2num(dmax)
450448

451449
return self.nonsingular(vmin, vmax)
452450

pandas/plotting/_matplotlib/core.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from pandas.core.frame import DataFrame
5656

5757
from pandas.io.formats.printing import pprint_thing
58+
from pandas.plotting._matplotlib import tools
5859
from pandas.plotting._matplotlib.converter import register_pandas_matplotlib_converters
5960
from pandas.plotting._matplotlib.groupby import reconstruct_data_with_by
6061
from pandas.plotting._matplotlib.misc import unpack_single_str_list
@@ -73,7 +74,6 @@
7374
get_all_lines,
7475
get_xlim,
7576
handle_shared_axes,
76-
table,
7777
)
7878

7979
if TYPE_CHECKING:
@@ -644,7 +644,7 @@ def _add_table(self) -> None:
644644
else:
645645
data = self.table
646646
ax = self._get_ax(0)
647-
table(ax, data)
647+
tools.table(ax, data)
648648

649649
def _post_plot_logic_common(self, ax, data):
650650
"""Common post process for each axes"""

pandas/plotting/_matplotlib/tools.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ def table(
7575

7676
cellText = data.values
7777

78-
table = matplotlib.table.table(
78+
return matplotlib.table.table(
7979
ax, cellText=cellText, rowLabels=rowLabels, colLabels=colLabels, **kwargs
8080
)
81-
return table
8281

8382

8483
def _get_layout(

pandas/tests/plotting/test_converter.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def test_conversion_float(self, dtc):
215215
rtol = 0.5 * 10**-9
216216

217217
rs = dtc.convert(Timestamp("2012-1-1 01:02:03", tz="UTC"), None, None)
218-
xp = converter.dates.date2num(Timestamp("2012-1-1 01:02:03", tz="UTC"))
218+
xp = converter.mdates.date2num(Timestamp("2012-1-1 01:02:03", tz="UTC"))
219219
tm.assert_almost_equal(rs, xp, rtol=rtol)
220220

221221
rs = dtc.convert(
@@ -230,18 +230,18 @@ def test_conversion_outofbounds_datetime(self, dtc):
230230
# 2579
231231
values = [date(1677, 1, 1), date(1677, 1, 2)]
232232
rs = dtc.convert(values, None, None)
233-
xp = converter.dates.date2num(values)
233+
xp = converter.mdates.date2num(values)
234234
tm.assert_numpy_array_equal(rs, xp)
235235
rs = dtc.convert(values[0], None, None)
236-
xp = converter.dates.date2num(values[0])
236+
xp = converter.mdates.date2num(values[0])
237237
assert rs == xp
238238

239239
values = [datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)]
240240
rs = dtc.convert(values, None, None)
241-
xp = converter.dates.date2num(values)
241+
xp = converter.mdates.date2num(values)
242242
tm.assert_numpy_array_equal(rs, xp)
243243
rs = dtc.convert(values[0], None, None)
244-
xp = converter.dates.date2num(values[0])
244+
xp = converter.mdates.date2num(values[0])
245245
assert rs == xp
246246

247247
@pytest.mark.parametrize(
@@ -264,7 +264,7 @@ def test_dateindex_conversion(self, freq, dtc):
264264
rtol = 10**-9
265265
dateindex = tm.makeDateIndex(k=10, freq=freq)
266266
rs = dtc.convert(dateindex, None, None)
267-
xp = converter.dates.date2num(dateindex._mpl_repr())
267+
xp = converter.mdates.date2num(dateindex._mpl_repr())
268268
tm.assert_almost_equal(rs, xp, rtol=rtol)
269269

270270
@pytest.mark.parametrize("offset", [Second(), Milli(), Micro(50)])

0 commit comments

Comments
 (0)