Skip to content

Commit deffee6

Browse files
xcz011WillAyd
authored andcommitted
move late imports to top (#26750)
1 parent f0a4c43 commit deffee6

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

pandas/plotting/_matplotlib/boxplot.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
import warnings
33

44
from matplotlib import pyplot as plt
5+
from matplotlib.artist import setp
56
import numpy as np
67

78
from pandas.core.dtypes.generic import ABCSeries
89
from pandas.core.dtypes.missing import remove_na_arraylike
910

11+
import pandas as pd
12+
1013
from pandas.io.formats.printing import pprint_thing
1114
from pandas.plotting._matplotlib.core import LinePlot, MPLPlot
1215
from pandas.plotting._matplotlib.style import _get_standard_colors
@@ -105,16 +108,14 @@ def maybe_color_bp(self, bp):
105108
medians = self.color or self._medians_c
106109
caps = self.color or self._caps_c
107110

108-
from matplotlib.artist import setp
109111
setp(bp['boxes'], color=boxes, alpha=1)
110112
setp(bp['whiskers'], color=whiskers, alpha=1)
111113
setp(bp['medians'], color=medians, alpha=1)
112114
setp(bp['caps'], color=caps, alpha=1)
113115

114116
def _make_plot(self):
115117
if self.subplots:
116-
from pandas.core.series import Series
117-
self._return_obj = Series()
118+
self._return_obj = pd.Series()
118119

119120
for i, (label, y) in enumerate(self._iter_data()):
120121
ax = self._get_ax(i)
@@ -197,8 +198,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
197198
ax_values.append(re_plotf)
198199
ax.grid(grid)
199200

200-
from pandas.core.series import Series
201-
result = Series(ax_values, index=columns)
201+
result = pd.Series(ax_values, index=columns)
202202

203203
# Return axes in multiplot case, maybe revisit later # 985
204204
if return_type is None:
@@ -230,7 +230,6 @@ def _get_colors():
230230

231231
def maybe_color_bp(bp):
232232
if 'color' not in kwds:
233-
from matplotlib.artist import setp
234233
setp(bp['boxes'], color=colors[0], alpha=1)
235234
setp(bp['whiskers'], color=colors[0], alpha=1)
236235
setp(bp['medians'], color=colors[2], alpha=1)
@@ -314,8 +313,7 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
314313
figsize=figsize, layout=layout)
315314
axes = _flatten(axes)
316315

317-
from pandas.core.series import Series
318-
ret = Series()
316+
ret = pd.Series()
319317
for (key, group), ax in zip(grouped, axes):
320318
d = group.boxplot(ax=ax, column=column, fontsize=fontsize,
321319
rot=rot, grid=grid, **kwds)
@@ -324,10 +322,9 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
324322
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1,
325323
right=0.9, wspace=0.2)
326324
else:
327-
from pandas.core.reshape.concat import concat
328325
keys, frames = zip(*grouped)
329326
if grouped.axis == 0:
330-
df = concat(frames, keys=keys, axis=1)
327+
df = pd.concat(frames, keys=keys, axis=1)
331328
else:
332329
if len(frames) > 1:
333330
df = frames[0].join(frames[1::])

pandas/plotting/_matplotlib/misc.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# being a bit too dynamic
1+
import random
2+
3+
import matplotlib.lines as mlines
4+
import matplotlib.patches as patches
25
import matplotlib.pyplot as plt
36
import numpy as np
47

@@ -96,14 +99,12 @@ def scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False,
9699

97100

98101
def _get_marker_compat(marker):
99-
import matplotlib.lines as mlines
100102
if marker not in mlines.lineMarkers:
101103
return 'o'
102104
return marker
103105

104106

105107
def radviz(frame, class_column, ax=None, color=None, colormap=None, **kwds):
106-
import matplotlib.patches as patches
107108

108109
def normalize(series):
109110
a = min(series)
@@ -168,12 +169,11 @@ def normalize(series):
168169

169170
def andrews_curves(frame, class_column, ax=None, samples=200, color=None,
170171
colormap=None, **kwds):
171-
from math import sqrt, pi
172172

173173
def function(amplitudes):
174174
def f(t):
175175
x1 = amplitudes[0]
176-
result = x1 / sqrt(2.0)
176+
result = x1 / np.sqrt(2.0)
177177

178178
# Take the rest of the coefficients and resize them
179179
# appropriately. Take a copy of amplitudes as otherwise numpy
@@ -196,15 +196,15 @@ def f(t):
196196
class_col = frame[class_column]
197197
classes = frame[class_column].drop_duplicates()
198198
df = frame.drop(class_column, axis=1)
199-
t = np.linspace(-pi, pi, samples)
199+
t = np.linspace(-np.pi, np.pi, samples)
200200
used_legends = set()
201201

202202
color_values = _get_standard_colors(num_colors=len(classes),
203203
colormap=colormap, color_type='random',
204204
color=color)
205205
colors = dict(zip(classes, color_values))
206206
if ax is None:
207-
ax = plt.gca(xlim=(-pi, pi))
207+
ax = plt.gca(xlim=(-np.pi, np.pi))
208208
for i in range(n):
209209
row = df.iloc[i].values
210210
f = function(row)
@@ -223,7 +223,6 @@ def f(t):
223223

224224

225225
def bootstrap_plot(series, fig=None, size=50, samples=500, **kwds):
226-
import random
227226

228227
# random.sample(ndarray, int) fails on python 3.3, sigh
229228
data = list(series.values)

pandas/plotting/_matplotlib/style.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
# being a bit too dynamic
22
import warnings
33

4+
import matplotlib.cm as cm
5+
import matplotlib.colors
46
import matplotlib.pyplot as plt
57
import numpy as np
68

79
from pandas.core.dtypes.common import is_list_like
810

11+
import pandas.core.common as com
12+
913

1014
def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
1115
color=None):
1216
if color is None and colormap is not None:
1317
if isinstance(colormap, str):
14-
import matplotlib.cm as cm
1518
cmap = colormap
1619
colormap = cm.get_cmap(colormap)
1720
if colormap is None:
@@ -37,7 +40,6 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
3740

3841
colors = colors[0:num_colors]
3942
elif color_type == 'random':
40-
import pandas.core.common as com
4143

4244
def random_color(column):
4345
""" Returns a random color represented as a list of length 3"""
@@ -50,7 +52,6 @@ def random_color(column):
5052
raise ValueError("color_type must be either 'default' or 'random'")
5153

5254
if isinstance(colors, str):
53-
import matplotlib.colors
5455
conv = matplotlib.colors.ColorConverter()
5556

5657
def _maybe_valid_colors(colors):

pandas/plotting/_matplotlib/timeseries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# TODO: Use the fact that axis can have units to simplify the process
22

33
import functools
4+
import warnings
45

56
from matplotlib import pylab
67
import matplotlib.pyplot as plt
@@ -25,7 +26,6 @@
2526

2627

2728
def tsplot(series, plotf, ax=None, **kwargs):
28-
import warnings
2929
"""
3030
Plots a Series on the given Matplotlib axes or the current axes
3131

pandas/plotting/_matplotlib/tools.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import warnings
44

55
import matplotlib.pyplot as plt
6+
import matplotlib.table
7+
import matplotlib.ticker as ticker
68
import numpy as np
79

810
from pandas.core.dtypes.common import is_list_like
@@ -37,7 +39,6 @@ def table(ax, data, rowLabels=None, colLabels=None, **kwargs):
3739

3840
cellText = data.values
3941

40-
import matplotlib.table
4142
table = matplotlib.table.table(ax, cellText=cellText,
4243
rowLabels=rowLabels,
4344
colLabels=colLabels, **kwargs)
@@ -260,7 +261,6 @@ def _remove_labels_from_axis(axis):
260261
try:
261262
# set_visible will not be effective if
262263
# minor axis has NullLocator and NullFormattor (default)
263-
import matplotlib.ticker as ticker
264264
if isinstance(axis.get_minor_locator(), ticker.NullLocator):
265265
axis.set_minor_locator(ticker.AutoLocator())
266266
if isinstance(axis.get_minor_formatter(), ticker.NullFormatter):

0 commit comments

Comments
 (0)