Skip to content

Commit 56e35a9

Browse files
committed
ENH: Series.name integration, working on sparse compat and adding unit
tests. Also refactored Series.__repr__
1 parent 7597471 commit 56e35a9

File tree

9 files changed

+163
-81
lines changed

9 files changed

+163
-81
lines changed

TODO.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
- Is there a way to write hierarchical columns to csv?
55
- Possible to blow away existing name when creating MultiIndex?
66
- prettytable output with index names
7+
- Add load/save functions to top level pandas namespace

pandas/core/frame.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,9 +2156,14 @@ def join(self, other, on=None, how=None, lsuffix='', rsuffix=''):
21562156
else:
21572157
if how is None:
21582158
how = 'left'
2159+
21592160
return self._join_index(other, how, lsuffix, rsuffix)
21602161

21612162
def _join_on(self, other, on, lsuffix, rsuffix):
2163+
if isinstance(other, Series):
2164+
assert(other.name is not None)
2165+
other = DataFrame({other.name : other})
2166+
21622167
if len(other.index) == 0:
21632168
return self
21642169

@@ -2169,6 +2174,10 @@ def _join_on(self, other, on, lsuffix, rsuffix):
21692174
def _join_index(self, other, how, lsuffix, rsuffix):
21702175
from pandas.core.internals import join_managers
21712176

2177+
if isinstance(other, Series):
2178+
assert(other.name is not None)
2179+
other = DataFrame({other.name : other})
2180+
21722181
thisdata, otherdata = self._data._maybe_rename_join(
21732182
other._data, lsuffix, rsuffix, copydata=False)
21742183

pandas/core/panel.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
"""
44
# pylint: disable=E1103,W0231,W0212,W0621
55

6-
from cStringIO import StringIO
76
import operator
8-
import sys
9-
import warnings
10-
117
import numpy as np
128

139
from pandas.core.common import (PandasError, _mut_exclusive,

pandas/core/series.py

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ def __init__(self, data, index=None, dtype=None, name=None, copy=False):
186186
"""
187187
pass
188188

189+
@property
190+
def _constructor(self):
191+
return Series
192+
189193
def __hash__(self):
190194
raise TypeError('unhashable type')
191195

@@ -304,7 +308,8 @@ def _multilevel_index(self, key):
304308
_get_val_at = ndarray.__getitem__
305309

306310
def __getslice__(self, i, j):
307-
return Series(self.values[i:j], index=self.index[i:j], name=self.name)
311+
return self._constructor(self.values[i:j], index=self.index[i:j],
312+
name=self.name)
308313

309314
def __setitem__(self, key, value):
310315
values = self.values
@@ -354,24 +359,52 @@ def __setslice__(self, i, j, value):
354359
def __repr__(self):
355360
"""Clean string representation of a Series"""
356361
if len(self.index) > 500:
357-
return self._make_repr(50)
362+
return self._tidy_repr(30)
358363
elif len(self.index) > 0:
359-
return _seriesRepr(self.index, self.values)
364+
return self._get_repr(name=True)
360365
else:
361366
return '%s' % ndarray.__repr__(self)
362367

363-
def _make_repr(self, max_vals=50):
368+
def _tidy_repr(self, max_vals=20):
369+
num = max_vals // 2
370+
head = self[:num]._get_repr(name=False)
371+
tail = self[-(max_vals - num):]._get_repr(name=False)
372+
result = head + '\n...\n' + tail
373+
result = '%s\nName: %s, Length: %d' % (result, self.name, len(self))
374+
return result
375+
376+
def to_string(self, buffer=sys.stdout, nanRep='NaN'):
377+
print >> buffer, self._get_repr(nanRep=nanRep)
378+
379+
def _get_repr(self, name=False, nanRep='NaN'):
364380
vals = self.values
365381
index = self.index
366382

367-
num = max_vals // 2
368-
head = _seriesRepr(index[:num], vals[:num])
369-
tail = _seriesRepr(index[-(max_vals - num):], vals[-(max_vals - num):])
370-
return head + '\n...\n' + tail + '\nlength: %d' % len(vals)
383+
string_index = index.format()
384+
maxlen = max(len(x) for x in string_index)
385+
padSpace = min(maxlen, 60)
371386

372-
def toString(self, buffer=sys.stdout, nanRep='NaN'):
373-
print >> buffer, _seriesRepr(self.index, self.values,
374-
nanRep=nanRep)
387+
def _format_float(k, v):
388+
if np.isnan(v):
389+
v = nanRep
390+
else:
391+
v = str(v)
392+
return '%s %s' % (str(k).ljust(padSpace), v)
393+
394+
def _format_nonfloat(k, v):
395+
return '%s %s' % (str(k).ljust(padSpace), v)
396+
397+
if vals.dtype == np.float_:
398+
_format = _format_float
399+
else:
400+
_format = _format_nonfloat
401+
402+
it = itertools.starmap(_format,
403+
itertools.izip(string_index, vals))
404+
it = list(it)
405+
if name:
406+
it.append('Name: %s, Length: %d' % (str(self.name), len(self)))
407+
return '\n'.join(it)
375408

376409
def __str__(self):
377410
return repr(self)
@@ -462,7 +495,8 @@ def to_sparse(self, kind='block', fill_value=None):
462495
sp : SparseSeries
463496
"""
464497
from pandas.core.sparse import SparseSeries
465-
return SparseSeries(self, kind=kind, fill_value=fill_value)
498+
return SparseSeries(self, kind=kind, fill_value=fill_value,
499+
name=self.name)
466500

467501
def get(self, key, default=None):
468502
"""
@@ -946,9 +980,9 @@ def append(self, other):
946980
new_index = self.index.append(other.index)
947981
new_index._verify_integrity()
948982

949-
new_values = np.concatenate((self, other))
983+
new_values = np.concatenate((self.values, other.values))
950984
name = _maybe_match_name(self, other)
951-
return Series(new_values, index=new_index, name=name)
985+
return self._constructor(new_values, index=new_index, name=name)
952986

953987
def _binop(self, other, func, fill_value=None):
954988
"""
@@ -1804,6 +1838,7 @@ def fromValue(cls, value=nan, index=None, dtype=None): # pragma: no cover
18041838

18051839
asOf = deprecate('asOf', asof)
18061840
toDict = deprecate('toDict', to_dict)
1841+
toString = deprecate('toString', to_string)
18071842
merge = deprecate('merge', map)
18081843
applymap = deprecate('applymap', apply)
18091844
combineFirst = deprecate('combineFirst', combine_first)
@@ -1822,28 +1857,3 @@ def remove_na(arr):
18221857
Return array containing only true/non-NaN values, possibly empty.
18231858
"""
18241859
return arr[notnull(arr)]
1825-
1826-
def _seriesRepr(index, vals, nanRep='NaN'):
1827-
string_index = index.format()
1828-
maxlen = max(len(x) for x in string_index)
1829-
padSpace = min(maxlen, 60)
1830-
1831-
if vals.dtype == np.object_:
1832-
def _format(k, v):
1833-
return '%s %s' % (str(k).ljust(padSpace), v)
1834-
elif vals.dtype == np.float_:
1835-
def _format(k, v):
1836-
if np.isnan(v):
1837-
v = nanRep
1838-
else:
1839-
v = str(v)
1840-
1841-
return '%s %s' % (str(k).ljust(padSpace), v)
1842-
else:
1843-
def _format(k, v):
1844-
return '%s %s' % (str(k).ljust(padSpace), v)
1845-
1846-
it = itertools.starmap(_format,
1847-
itertools.izip(string_index, vals))
1848-
1849-
return '\n'.join(it)

0 commit comments

Comments
 (0)