Skip to content

Commit 7222e5a

Browse files
committed
CLN: Have PyTables, stats, & Stata use StringMixin
CLN: Make PyTables unicode safe + add StringMixin CLN: Make StataMissingValue use StringMixin ENH: Use StringMixin for addl string methods in stats
1 parent 0cf93aa commit 7222e5a

File tree

5 files changed

+37
-36
lines changed

5 files changed

+37
-36
lines changed

pandas/io/pytables.py

+21-25
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from pandas.sparse.api import SparseSeries, SparseDataFrame, SparsePanel
1818
from pandas.sparse.array import BlockIndex, IntIndex
1919
from pandas.tseries.api import PeriodIndex, DatetimeIndex
20-
from pandas.core.common import adjoin, is_list_like
20+
from pandas.core.base import StringMixin
21+
from pandas.core.common import adjoin, is_list_like, pprint_thing
2122
from pandas.core.algorithms import match, unique
2223
from pandas.core.categorical import Categorical
2324
from pandas.core.common import _asarray_tuplesafe
@@ -218,7 +219,7 @@ def read_hdf(path_or_buf, key, **kwargs):
218219
# a passed store; user controls open/close
219220
f(path_or_buf, False)
220221

221-
class HDFStore(object):
222+
class HDFStore(StringMixin):
222223
"""
223224
dict-like IO interface for storing pandas objects in PyTables
224225
format.
@@ -315,8 +316,8 @@ def __contains__(self, key):
315316
def __len__(self):
316317
return len(self.groups())
317318

318-
def __repr__(self):
319-
output = '%s\nFile path: %s\n' % (type(self), self._path)
319+
def __unicode__(self):
320+
output = '%s\nFile path: %s\n' % (type(self), pprint_thing(self._path))
320321

321322
if len(self.keys()):
322323
keys = []
@@ -326,11 +327,11 @@ def __repr__(self):
326327
try:
327328
s = self.get_storer(k)
328329
if s is not None:
329-
keys.append(str(s.pathname or k))
330-
values.append(str(s or 'invalid_HDFStore node'))
331-
except (Exception), detail:
330+
keys.append(pprint_thing(s.pathname or k))
331+
values.append(pprint_thing(s or 'invalid_HDFStore node'))
332+
except Exception as detail:
332333
keys.append(k)
333-
values.append("[invalid_HDFStore node: %s]" % str(detail))
334+
values.append("[invalid_HDFStore node: %s]" % pprint_thing(detail))
334335

335336
output += adjoin(12, keys, values)
336337
else:
@@ -984,7 +985,7 @@ def get_values(self):
984985
self.close()
985986
return results
986987

987-
class IndexCol(object):
988+
class IndexCol(StringMixin):
988989
""" an index column description class
989990
990991
Parameters
@@ -1050,10 +1051,9 @@ def set_table(self, table):
10501051
self.table = table
10511052
return self
10521053

1053-
def __repr__(self):
1054-
return "name->%s,cname->%s,axis->%s,pos->%s,kind->%s" % (self.name, self.cname, self.axis, self.pos, self.kind)
1055-
1056-
__str__ = __repr__
1054+
def __unicode__(self):
1055+
temp = tuple(map(pprint_thing, (self.name, self.cname, self.axis, self.pos, self.kind)))
1056+
return "name->%s,cname->%s,axis->%s,pos->%s,kind->%s" % temp
10571057

10581058
def __eq__(self, other):
10591059
""" compare 2 col items """
@@ -1570,7 +1570,7 @@ class GenericDataIndexableCol(DataIndexableCol):
15701570
def get_attr(self):
15711571
pass
15721572

1573-
class Storer(object):
1573+
class Storer(StringMixin):
15741574
""" represent an object in my store
15751575
facilitate read/write of various types of objects
15761576
this is an abstract base class
@@ -1610,19 +1610,16 @@ def set_version(self):
16101610
def pandas_type(self):
16111611
return _ensure_decoded(getattr(self.group._v_attrs, 'pandas_type', None))
16121612

1613-
def __repr__(self):
1614-
""" return a pretty representatgion of myself """
1613+
def __unicode__(self):
1614+
""" return a pretty representation of myself """
16151615
self.infer_axes()
16161616
s = self.shape
16171617
if s is not None:
16181618
if isinstance(s, (list,tuple)):
1619-
s = "[%s]" % ','.join([ str(x) for x in s ])
1619+
s = "[%s]" % ','.join([pprint_thing(x) for x in s])
16201620
return "%-12.12s (shape->%s)" % (self.pandas_type,s)
16211621
return self.pandas_type
16221622

1623-
def __str__(self):
1624-
return self.__repr__()
1625-
16261623
def set_object_info(self):
16271624
""" set my pandas type & version """
16281625
self.attrs.pandas_type = self.pandas_kind
@@ -3435,7 +3432,7 @@ def _need_convert(kind):
34353432
return True
34363433
return False
34373434

3438-
class Term(object):
3435+
class Term(StringMixin):
34393436
"""create a term object that holds a field, op, and value
34403437
34413438
Parameters
@@ -3540,10 +3537,9 @@ def __init__(self, field, op=None, value=None, queryables=None, encoding=None):
35403537
if len(self.q):
35413538
self.eval()
35423539

3543-
def __str__(self):
3544-
return "field->%s,op->%s,value->%s" % (self.field, self.op, self.value)
3545-
3546-
__repr__ = __str__
3540+
def __unicode__(self):
3541+
attrs = map(pprint_thing, (self.field, self.op, self.value))
3542+
return "field->%s,op->%s,value->%s" % tuple(attrs)
35473543

35483544
@property
35493545
def is_valid(self):

pandas/io/stata.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import sys
1717
import struct
18+
from pandas.core.base import StringMixin
1819
from pandas.core.frame import DataFrame
1920
from pandas.core.series import Series
2021
from pandas.core.categorical import Categorical
@@ -163,7 +164,7 @@ def _datetime_to_stata_elapsed(date, fmt):
163164
raise ValueError("fmt %s not understood" % fmt)
164165

165166

166-
class StataMissingValue(object):
167+
class StataMissingValue(StringMixin):
167168
"""
168169
An observation's missing value.
169170
@@ -192,10 +193,12 @@ def __init__(self, offset, value):
192193
string = property(lambda self: self._str, doc="The Stata representation of the missing value: '.', '.a'..'.z'")
193194
value = property(lambda self: self._value, doc='The binary representation of the missing value.')
194195

195-
def __str__(self):
196-
return self._str
196+
def __unicode__(self):
197+
return self.string
197198

198-
__str__.__doc__ = string.__doc__
199+
def __repr__(self):
200+
# not perfect :-/
201+
return "%s(%s)" % (self.__class__, self)
199202

200203

201204
class StataParser(object):

pandas/stats/fama_macbeth.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pandas.core.base import StringMixin
12
from pandas.util.py3compat import StringIO
23

34
import numpy as np
@@ -26,7 +27,7 @@ def fama_macbeth(**kwargs):
2627
return klass(**kwargs)
2728

2829

29-
class FamaMacBeth(object):
30+
class FamaMacBeth(StringMixin):
3031
def __init__(self, y, x, intercept=True, nw_lags=None,
3132
nw_lags_beta=None,
3233
entity_effects=False, time_effects=False, x_effects=None,
@@ -114,7 +115,7 @@ def _coef_table(self):
114115

115116
return buffer.getvalue()
116117

117-
def __repr__(self):
118+
def __unicode__(self):
118119
return self.summary
119120

120121
@cache_readonly

pandas/stats/ols.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numpy as np
1111

1212
from pandas.core.api import DataFrame, Series, isnull
13+
from pandas.core.base import StringMixin
1314
from pandas.core.common import _ensure_float64
1415
from pandas.core.index import MultiIndex
1516
from pandas.core.panel import Panel
@@ -22,7 +23,7 @@
2223
_FP_ERR = 1e-8
2324

2425

25-
class OLS(object):
26+
class OLS(StringMixin):
2627
"""
2728
Runs a full sample ordinary least squares regression.
2829
@@ -581,7 +582,7 @@ def summary(self):
581582

582583
return template % params
583584

584-
def __repr__(self):
585+
def __unicode__(self):
585586
return self.summary
586587

587588
@cache_readonly

pandas/stats/var.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import division
22

33
import numpy as np
4-
4+
from pandas.core.base import StringMixin
55
from pandas.util.decorators import cache_readonly
66
from pandas.core.frame import DataFrame
77
from pandas.core.panel import Panel
@@ -11,7 +11,7 @@
1111
from pandas.stats.ols import _combine_rhs
1212

1313

14-
class VAR(object):
14+
class VAR(StringMixin):
1515
"""
1616
Estimates VAR(p) regression on multivariate time series data
1717
presented in pandas data structures.
@@ -477,7 +477,7 @@ def _sigma(self):
477477

478478
return np.dot(resid, resid.T) / (n - k)
479479

480-
def __repr__(self):
480+
def __unicode__(self):
481481
return self.summary
482482

483483

0 commit comments

Comments
 (0)