Skip to content

Commit 5fa2ae4

Browse files
author
y-p
committed
CLN: Abolish stringify and _strify in favor of pprint_thing()
1 parent 95678eb commit 5fa2ae4

File tree

6 files changed

+30
-61
lines changed

6 files changed

+30
-61
lines changed

pandas/core/common.py

-12
Original file line numberDiff line numberDiff line change
@@ -721,18 +721,6 @@ def _index_labels_to_array(labels):
721721

722722
return labels
723723

724-
def _stringify(col, encoding='UTF8'):
725-
# unicode workaround
726-
try:
727-
return unicode(col)
728-
except UnicodeError:
729-
try:
730-
if isinstance(col, str):
731-
return col.decode(encoding)
732-
except UnicodeError:
733-
pass
734-
return console_encode(col)
735-
736724
def _maybe_make_list(obj):
737725
if obj is not None and not isinstance(obj, (tuple, list)):
738726
return [obj]

pandas/core/format.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
except:
99
from io import StringIO
1010

11-
from pandas.core.common import adjoin, isnull, notnull, _stringify
11+
from pandas.core.common import adjoin, isnull, notnull
1212
from pandas.core.index import MultiIndex, _ensure_index
1313
from pandas.util import py3compat
1414

@@ -717,12 +717,7 @@ def _format_strings(self, use_unicode=False):
717717
else:
718718
float_format = self.float_format
719719

720-
if use_unicode:
721-
def _strify(x):
722-
return _stringify(x, print_config.encoding)
723-
formatter = _strify if self.formatter is None else self.formatter
724-
else:
725-
formatter = str if self.formatter is None else self.formatter
720+
formatter = com.pprint_thing if self.formatter is None else self.formatter
726721

727722
def _format(x):
728723
if self.na_rep is not None and lib.checknull(x):

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import numpy as np
2525
import numpy.ma as ma
2626

27-
from pandas.core.common import (isnull, notnull, PandasError, _try_sort,
28-
_default_index,_stringify,_is_sequence)
27+
from pandas.core.common import (isnull, notnull, PandasError, _try_sort,\
28+
_default_index,_is_sequence)
2929
from pandas.core.generic import NDFrame
3030
from pandas.core.index import Index, MultiIndex, _ensure_index
3131
from pandas.core.indexing import _NDFrameIndexer, _maybe_droplevels
@@ -1368,7 +1368,7 @@ def info(self, verbose=True, buf=None):
13681368
# hack
13691369
if verbose and len(self.columns) < 100:
13701370
lines.append('Data columns:')
1371-
space = max([len(_stringify(k)) for k in self.columns]) + 4
1371+
space = max([len(com.pprint_thing(k)) for k in self.columns]) + 4
13721372
counts = self.count()
13731373
assert(len(cols) == len(counts))
13741374
for col, count in counts.iteritems():

pandas/core/index.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1474,15 +1474,9 @@ def get_level_values(self, level):
14741474

14751475
def format(self, space=2, sparsify=None, adjoin=True, names=False,
14761476
na_rep='NaN'):
1477-
from pandas.core.common import _stringify
1478-
from pandas.core.format import print_config
1479-
def _strify(x):
1480-
return _stringify(x, print_config.encoding)
1481-
14821477
if len(self) == 0:
14831478
return []
14841479

1485-
14861480
stringified_levels = []
14871481
for lev, lab in zip(self.levels, self.labels):
14881482
if len(lev) > 0:
@@ -1497,7 +1491,7 @@ def _strify(x):
14971491
level = []
14981492

14991493
if names:
1500-
level.append(_strify(name) if name is not None else '')
1494+
level.append(com.pprint_thing(name) if name is not None else '')
15011495

15021496
level.extend(np.array(lev, dtype=object))
15031497
result_levels.append(level)

pandas/tools/plotting.py

+23-31
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def normalize(series):
207207
line = ax.scatter(to_plot[class_][0],
208208
to_plot[class_][1],
209209
color=random_color(class_),
210-
label=com._stringify(class_), **kwds)
210+
label=com.pprint_thing(class_), **kwds)
211211
ax.legend()
212212

213213
ax.add_patch(patches.Circle((0.0, 0.0), radius=1.0, facecolor='none'))
@@ -272,8 +272,8 @@ def random_color(column):
272272
f = function(row)
273273
y = [f(t) for t in x]
274274
label = None
275-
if com._stringify(class_col[i]) not in used_legends:
276-
label = com._stringify(class_col[i])
275+
if com.pprint_thing(class_col[i]) not in used_legends:
276+
label = com.pprint_thing(class_col[i])
277277
used_legends.add(label)
278278
ax.plot(x, y, color=random_color(class_col[i]), label=label)
279279
ax.legend(loc='upper right')
@@ -378,8 +378,8 @@ def random_color(column):
378378
y = row
379379
label = None
380380
kls = class_col.iget_value(i)
381-
if com._stringify(kls) not in used_legends:
382-
label = com._stringify(kls)
381+
if com.pprint_thing(kls) not in used_legends:
382+
label = com.pprint_thing(kls)
383383
used_legends.add(label)
384384
ax.plot(x, y, color=random_color(kls), label=label, **kwds)
385385

@@ -671,7 +671,7 @@ def _adorn_subplots(self):
671671
self.axes[0].set_title(self.title)
672672

673673
if self._need_to_set_index:
674-
labels = [_stringify(key) for key in self.data.index]
674+
labels = [com.pprint_thing(key) for key in self.data.index]
675675
labels = dict(zip(range(len(self.data.index)), labels))
676676

677677
for ax_ in self.axes:
@@ -685,10 +685,10 @@ def legend_title(self):
685685
if not isinstance(self.data.columns, MultiIndex):
686686
name = self.data.columns.name
687687
if name is not None:
688-
name = com._stringify(name)
688+
name = com.pprint_thing(name)
689689
return name
690690
else:
691-
stringified = map(com._stringify,
691+
stringified = map(com.pprint_thing,
692692
self.data.columns.names)
693693
return ','.join(stringified)
694694
else:
@@ -742,13 +742,13 @@ def _get_index_name(self):
742742
if isinstance(self.data.index, MultiIndex):
743743
name = self.data.index.names
744744
if any(x is not None for x in name):
745-
name = ','.join([com._stringify(x) for x in name])
745+
name = ','.join([com.pprint_thing(x) for x in name])
746746
else:
747747
name = None
748748
else:
749749
name = self.data.index.name
750750
if name is not None:
751-
name = com._stringify(name)
751+
name = com.pprint_thing(name)
752752

753753
return name
754754

@@ -806,7 +806,7 @@ def _make_plot(self):
806806
ax = self._get_ax(i)
807807
style = self._get_style(i, label)
808808

809-
label = com._stringify(label)
809+
label = com.pprint_thing(label)
810810

811811
gkde = gaussian_kde(y)
812812
sample_range = max(y) - min(y)
@@ -902,7 +902,7 @@ def _maybe_add_color(kwargs, style, i):
902902

903903
_maybe_add_color(kwds, style, i)
904904

905-
label = _stringify(label)
905+
label = com.pprint_thing(label).encode('utf-8')
906906

907907
mask = com.isnull(y)
908908
if mask.any():
@@ -947,7 +947,7 @@ def to_leg_label(label, i):
947947
if isinstance(data, Series):
948948
ax = self._get_ax(0) #self.axes[0]
949949
style = self.style or ''
950-
label = com._stringify(self.label)
950+
label = com.pprint_thing(self.label)
951951
kwds = kwargs.copy()
952952
_maybe_add_color(kwds, style, 0)
953953

@@ -959,7 +959,7 @@ def to_leg_label(label, i):
959959
labels.append(leg_label)
960960
else:
961961
for i, col in enumerate(data.columns):
962-
label = com._stringify(col)
962+
label = com.pprint_thing(col)
963963
ax = self._get_ax(i)
964964
style = self._get_style(i, col)
965965
kwds = kwargs.copy()
@@ -1097,7 +1097,7 @@ def _make_plot(self):
10971097
K = self.nseries
10981098

10991099
for i, (label, y) in enumerate(self._iter_data()):
1100-
label = com._stringify(label)
1100+
label = com.pprint_thing(label)
11011101
kwds = self.kwds.copy()
11021102
kwds['color'] = colors[i % len(colors)]
11031103

@@ -1125,7 +1125,7 @@ def _make_plot(self):
11251125

11261126
def _post_plot_logic(self):
11271127
for ax in self.axes:
1128-
str_index = [_stringify(key) for key in self.data.index]
1128+
str_index = [com.pprint_thing(key) for key in self.data.index]
11291129

11301130
name = self._get_index_name()
11311131
if self.kind == 'bar':
@@ -1359,7 +1359,7 @@ def boxplot(data, column=None, by=None, ax=None, fontsize=None,
13591359

13601360
def plot_group(grouped, ax):
13611361
keys, values = zip(*grouped)
1362-
keys = [_stringify(x) for x in keys]
1362+
keys = [com.pprint_thing(x) for x in keys]
13631363
values = [remove_na(v) for v in values]
13641364
ax.boxplot(values, **kwds)
13651365
if kwds.get('vert', 1):
@@ -1394,7 +1394,7 @@ def plot_group(grouped, ax):
13941394
cols = columns
13951395
else:
13961396
cols = data.columns
1397-
keys = [_stringify(x) for x in cols]
1397+
keys = [com.pprint_thing(x) for x in cols]
13981398

13991399
# Return boxplot dict in single plot case
14001400

@@ -1411,14 +1411,6 @@ def plot_group(grouped, ax):
14111411
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
14121412
return ret
14131413

1414-
1415-
def _stringify(x):
1416-
if isinstance(x, tuple):
1417-
return '|'.join(com._stringify(y) for y in x)
1418-
else:
1419-
return com._stringify(x)
1420-
1421-
14221414
def format_date_labels(ax, rot):
14231415
# mini version of autofmt_xdate
14241416
try:
@@ -1455,8 +1447,8 @@ def plot_group(group, ax):
14551447
else:
14561448
fig = ax.get_figure()
14571449
plot_group(data, ax)
1458-
ax.set_ylabel(com._stringify(y))
1459-
ax.set_xlabel(com._stringify(x))
1450+
ax.set_ylabel(com.pprint_thing(y))
1451+
ax.set_xlabel(com.pprint_thing(x))
14601452

14611453
ax.grid(grid)
14621454

@@ -1620,7 +1612,7 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
16201612
for (key, group), ax in zip(grouped, axes):
16211613
d = group.boxplot(ax=ax, column=column, fontsize=fontsize,
16221614
rot=rot, grid=grid, figsize=figsize, **kwds)
1623-
ax.set_title(_stringify(key))
1615+
ax.set_title(com.pprint_thing(key))
16241616
ret[key] = d
16251617
else:
16261618
from pandas.tools.merge import concat
@@ -1676,7 +1668,7 @@ def _grouped_plot(plotf, data, column=None, by=None, numeric_only=True,
16761668
if numeric_only and isinstance(group, DataFrame):
16771669
group = group._get_numeric_data()
16781670
plotf(group, ax)
1679-
ax.set_title(com._stringify(key))
1671+
ax.set_title(com.pprint_thing(key))
16801672

16811673
return fig, axes
16821674

@@ -1710,7 +1702,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
17101702
gp_col = grouped[col]
17111703
plotf(gp_col, ax)
17121704
ax.set_title(col)
1713-
ax.set_xlabel(com._stringify(by))
1705+
ax.set_xlabel(com.pprint_thing(by))
17141706
ax.grid(grid)
17151707

17161708
byline = by[0] if len(by) == 1 else by

pandas/tseries/plotting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def _replot_ax(ax, freq, plotf, kwargs):
158158
ax._plot_data.append(series)
159159
args = _maybe_mask(series)
160160
lines.append(plotf(ax, *args, **kwds)[0])
161-
labels.append(com._stringify(series.name))
161+
labels.append(com.pprint_thing(series.name))
162162

163163
return lines, labels
164164

0 commit comments

Comments
 (0)