Skip to content

Commit 0cf93aa

Browse files
committed
CLN: Make more core objects inherit PandasObject
CLN: Make Categorical inherit from PandasObject CLN: Make GroupBy inherit from PandasObject CLN/ENH: Make Sparse* into PandasObjects Plus get all the string methods working... CLN: Index now a PandasObject + str method cleanup CLN: Make tseries/index fit with PandasObject. CLN: Use PandasObject in internals + cleanup CLN: Make Period into a PandasObject + cleanup CLN: Remove extraneous __repr__ from io/excel
1 parent 411d13f commit 0cf93aa

File tree

10 files changed

+38
-118
lines changed

10 files changed

+38
-118
lines changed

pandas/core/categorical.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44

55
from pandas.core.algorithms import factorize
6+
from pandas.core.base import PandasObject
67
from pandas.core.index import Index
78
import pandas.core.common as com
89
from pandas.core.frame import DataFrame
@@ -25,8 +26,7 @@ def f(self, other):
2526

2627
return f
2728

28-
29-
class Categorical(object):
29+
class Categorical(PandasObject):
3030
"""
3131
Represents a categorical variable in classic R / S-plus fashion
3232
@@ -134,9 +134,9 @@ def __array__(self, dtype=None):
134134
def __len__(self):
135135
return len(self.labels)
136136

137-
def __repr__(self):
137+
def __unicode__(self):
138138
temp = 'Categorical: %s\n%s\n%s'
139-
values = np.asarray(self)
139+
values = com.pprint_thing(np.asarray(self))
140140
levheader = 'Levels (%d): ' % len(self.levels)
141141
levstring = np.array_repr(self.levels,
142142
max_line_width=60)
@@ -145,9 +145,9 @@ def __repr__(self):
145145
lines = levstring.split('\n')
146146
levstring = '\n'.join([lines[0]] +
147147
[indent + x.lstrip() for x in lines[1:]])
148+
name = '' if self.name is None else self.name
149+
return temp % (name, values, levheader + levstring)
148150

149-
return temp % ('' if self.name is None else self.name,
150-
repr(values), levheader + levstring)
151151

152152
def __getitem__(self, key):
153153
if isinstance(key, (int, np.integer)):

pandas/core/groupby.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import types
33
import numpy as np
44

5+
from pandas.core.base import PandasObject
56
from pandas.core.categorical import Categorical
67
from pandas.core.frame import DataFrame
78
from pandas.core.generic import NDFrame
@@ -100,7 +101,7 @@ def _last(x):
100101
return _last(x)
101102

102103

103-
class GroupBy(object):
104+
class GroupBy(PandasObject):
104105
"""
105106
Class for grouping and aggregating relational data. See aggregate,
106107
transform, and apply functions on this object.
@@ -201,6 +202,10 @@ def __init__(self, obj, keys=None, axis=0, level=None,
201202
def __len__(self):
202203
return len(self.indices)
203204

205+
def __unicode__(self):
206+
# TODO: Better unicode/repr for GroupBy object
207+
return object.__repr__(self)
208+
204209
@property
205210
def groups(self):
206211
return self.grouper.groups

pandas/core/index.py

+2-61
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pandas.algos as _algos
1010
import pandas.index as _index
1111
from pandas.lib import Timestamp
12+
from pandas.core.base import PandasObject
1213

1314
from pandas.util.decorators import cache_readonly
1415
from pandas.core.common import isnull
@@ -47,7 +48,7 @@ def _shouldbe_timestamp(obj):
4748
or tslib.is_timestamp_array(obj))
4849

4950

50-
class Index(np.ndarray):
51+
class Index(PandasObject, np.ndarray):
5152
"""
5253
Immutable ndarray implementing an ordered, sliceable set. The basic object
5354
storing axis labels for all pandas objects
@@ -142,28 +143,6 @@ def __array_finalize__(self, obj):
142143
def _shallow_copy(self):
143144
return self.view()
144145

145-
def __str__(self):
146-
"""
147-
Return a string representation for a particular Index
148-
149-
Invoked by str(df) in both py2/py3.
150-
Yields Bytestring in Py2, Unicode String in py3.
151-
"""
152-
153-
if py3compat.PY3:
154-
return self.__unicode__()
155-
return self.__bytes__()
156-
157-
def __bytes__(self):
158-
"""
159-
Return a string representation for a particular Index
160-
161-
Invoked by bytes(df) in py3 only.
162-
Yields a bytestring in both py2/py3.
163-
"""
164-
encoding = com.get_option("display.encoding")
165-
return self.__unicode__().encode(encoding, 'replace')
166-
167146
def __unicode__(self):
168147
"""
169148
Return a string representation for a particular Index
@@ -173,14 +152,6 @@ def __unicode__(self):
173152
prepr = com.pprint_thing(self, escape_chars=('\t', '\r', '\n'),quote_strings=True)
174153
return '%s(%s, dtype=%s)' % (type(self).__name__, prepr, self.dtype)
175154

176-
def __repr__(self):
177-
"""
178-
Return a string representation for a particular Index
179-
180-
Yields Bytestring in Py2, Unicode String in py3.
181-
"""
182-
return str(self)
183-
184155
def to_series(self):
185156
"""
186157
return a series with both index and values equal to the index keys
@@ -1531,28 +1502,6 @@ def _array_values(self):
15311502
def dtype(self):
15321503
return np.dtype('O')
15331504

1534-
def __str__(self):
1535-
"""
1536-
Return a string representation for a particular Index
1537-
1538-
Invoked by str(df) in both py2/py3.
1539-
Yields Bytestring in Py2, Unicode String in py3.
1540-
"""
1541-
1542-
if py3compat.PY3:
1543-
return self.__unicode__()
1544-
return self.__bytes__()
1545-
1546-
def __bytes__(self):
1547-
"""
1548-
Return a string representation for a particular Index
1549-
1550-
Invoked by bytes(df) in py3 only.
1551-
Yields a bytestring in both py2/py3.
1552-
"""
1553-
encoding = com.get_option("display.encoding")
1554-
return self.__unicode__().encode(encoding, 'replace')
1555-
15561505
def __unicode__(self):
15571506
"""
15581507
Return a string representation for a particular Index
@@ -1566,14 +1515,6 @@ def __unicode__(self):
15661515

15671516
return output % summary
15681517

1569-
def __repr__(self):
1570-
"""
1571-
Return a string representation for a particular Index
1572-
1573-
Yields Bytestring in Py2, Unicode String in py3.
1574-
"""
1575-
return str(self)
1576-
15771518
def __len__(self):
15781519
return len(self.labels[0])
15791520

pandas/core/internals.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from numpy import nan
66
import numpy as np
7+
from pandas.core.base import PandasObject
78

89
from pandas.core.common import (_possibly_downcast_to_dtype, isnull, _NS_DTYPE,
910
_TD_DTYPE)
@@ -19,7 +20,7 @@
1920
from pandas.util import py3compat
2021

2122

22-
class Block(object):
23+
class Block(PandasObject):
2324
"""
2425
Canonical n-dimensional unit of homogeneous dtype contained in a pandas
2526
data structure
@@ -91,14 +92,12 @@ def set_ref_items(self, ref_items, maybe_rename=True):
9192
self.items = ref_items.take(self.ref_locs)
9293
self.ref_items = ref_items
9394

94-
def __repr__(self):
95+
def __unicode__(self):
9596
shape = ' x '.join([com.pprint_thing(s) for s in self.shape])
9697
name = type(self).__name__
9798
result = '%s: %s, %s, dtype %s' % (
9899
name, com.pprint_thing(self.items), shape, self.dtype)
99-
if py3compat.PY3:
100-
return unicode(result)
101-
return com.console_encode(result)
100+
return result
102101

103102
def __contains__(self, item):
104103
return item in self.items
@@ -969,7 +968,7 @@ def make_block(values, items, ref_items, klass=None, fastpath=False, placement=N
969968
# TODO: flexible with index=None and/or items=None
970969

971970

972-
class BlockManager(object):
971+
class BlockManager(PandasObject):
973972
"""
974973
Core internal data structure to implement DataFrame
975974
@@ -1213,7 +1212,7 @@ def __setstate__(self, state):
12131212
def __len__(self):
12141213
return len(self.items)
12151214

1216-
def __repr__(self):
1215+
def __unicode__(self):
12171216
output = 'BlockManager'
12181217
for i, ax in enumerate(self.axes):
12191218
if i == 0:
@@ -1222,7 +1221,7 @@ def __repr__(self):
12221221
output += '\nAxis %d: %s' % (i, ax)
12231222

12241223
for block in self.blocks:
1225-
output += '\n%s' % repr(block)
1224+
output += '\n%s' % com.pprint_thing(block)
12261225
return output
12271226

12281227
@property

pandas/io/excel.py

-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ def __init__(self, path_or_buf, kind=None, **kwds):
7373
data = path_or_buf.read()
7474
self.book = xlrd.open_workbook(file_contents=data)
7575

76-
def __repr__(self):
77-
return object.__repr__(self)
78-
7976
def parse(self, sheetname, header=0, skiprows=None, skip_footer=0,
8077
index_col=None, parse_cols=None, parse_dates=False,
8178
date_parser=None, na_values=None, thousands=None, chunksize=None,

pandas/sparse/array.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99

1010
import operator
11+
from pandas.core.base import PandasObject
1112
import pandas.core.common as com
1213

1314
from pandas.util import py3compat
@@ -86,8 +87,7 @@ def _sparse_fillop(this, other, name):
8687

8788
return result, result_index
8889

89-
90-
class SparseArray(np.ndarray):
90+
class SparseArray(PandasObject, np.ndarray):
9191
"""Data structure for labeled, sparse floating point data
9292
9393
Parameters
@@ -184,9 +184,9 @@ def __setstate__(self, state):
184184
def __len__(self):
185185
return self.sp_index.length
186186

187-
def __repr__(self):
188-
return '%s\n%s' % (np.ndarray.__repr__(self),
189-
repr(self.sp_index))
187+
def __unicode__(self):
188+
return '%s\n%s' % (com.pprint_thing(self),
189+
com.pprint_thing(self.sp_index))
190190

191191
# Arithmetic operators
192192

pandas/sparse/list.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import numpy as np
2+
from pandas.core.base import PandasObject
3+
from pandas.core.common import pprint_thing
24

35
from pandas.sparse.array import SparseArray
46
import pandas._sparse as splib
57

68

7-
class SparseList(object):
9+
class SparseList(PandasObject):
810
"""
911
Data structure for accumulating data to be converted into a
1012
SparseArray. Has similar API to the standard Python list
@@ -21,9 +23,9 @@ def __init__(self, data=None, fill_value=np.nan):
2123
if data is not None:
2224
self.append(data)
2325

24-
def __repr__(self):
26+
def __unicode__(self):
2527
contents = '\n'.join(repr(c) for c in self._chunks)
26-
return '%s\n%s' % (object.__repr__(self), contents)
28+
return '%s\n%s' % (object.__repr__(self), pprint_thing(contents))
2729

2830
def __len__(self):
2931
return sum(len(c) for c in self._chunks)

pandas/sparse/series.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ def __setstate__(self, state):
241241
def __len__(self):
242242
return self.sp_index.length
243243

244-
def __repr__(self):
245-
series_rep = Series.__repr__(self)
244+
def __unicode__(self):
245+
# currently, unicode is same as repr...fixes infinite loop
246+
series_rep = Series.__unicode__(self)
246247
rep = '%s\n%s' % (series_rep, repr(self.sp_index))
247248
return rep
248249

pandas/tseries/index.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def _mpl_repr(self):
488488
# how to represent ourselves to matplotlib
489489
return tslib.ints_to_pydatetime(self.asi8, self.tz)
490490

491-
def __repr__(self):
491+
def __unicode__(self):
492492
from pandas.core.format import _format_datetime64
493493
values = self.values
494494

@@ -514,8 +514,6 @@ def __repr__(self):
514514

515515
return summary
516516

517-
__str__ = __repr__
518-
519517
def __reduce__(self):
520518
"""Necessary for making this object picklable"""
521519
object_state = list(np.ndarray.__reduce__(self))

pandas/tseries/period.py

+3-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from datetime import datetime, date
55
import numpy as np
6+
from pandas.core.base import PandasObject
67

78
import pandas.tseries.offsets as offsets
89
from pandas.tseries.frequencies import (get_freq_code as _gfc,
@@ -40,7 +41,7 @@ def f(self):
4041
return property(f)
4142

4243

43-
class Period(object):
44+
class Period(PandasObject):
4445
"""
4546
Represents an period of time
4647
@@ -272,28 +273,6 @@ def __repr__(self):
272273

273274
return "Period('%s', '%s')" % (formatted, freqstr)
274275

275-
def __str__(self):
276-
"""
277-
Return a string representation for a particular DataFrame
278-
279-
Invoked by str(df) in both py2/py3.
280-
Yields Bytestring in Py2, Unicode String in py3.
281-
"""
282-
283-
if py3compat.PY3:
284-
return self.__unicode__()
285-
return self.__bytes__()
286-
287-
def __bytes__(self):
288-
"""
289-
Return a string representation for a particular DataFrame
290-
291-
Invoked by bytes(df) in py3 only.
292-
Yields a bytestring in both py2/py3.
293-
"""
294-
encoding = com.get_option("display.encoding")
295-
return self.__unicode__().encode(encoding, 'replace')
296-
297276
def __unicode__(self):
298277
"""
299278
Return a string representation for a particular DataFrame
@@ -303,9 +282,7 @@ def __unicode__(self):
303282
"""
304283
base, mult = _gfc(self.freq)
305284
formatted = tslib.period_format(self.ordinal, base)
306-
value = (u"%s" % formatted)
307-
assert type(value) == unicode
308-
285+
value = ("%s" % formatted)
309286
return value
310287

311288

0 commit comments

Comments
 (0)