Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions sparsity/sparse_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,24 +491,23 @@ def __repr__(self):
data = data.toarray()
else:
cols = self._columns
data = self.data[:nrows,:].toarray()
data = self.data[:nrows, :].toarray()

df = pd.DataFrame(data, columns=cols, index=self._index[:nrows])
df_str = df.__repr__().splitlines()
if df_str[-2] == '':
df_str = df_str[:-2]

df = pd.DataFrame(data,
columns=cols,
index=self._index[:nrows]
)
df_str = df.__repr__().splitlines()[:-2]
sparse_str = "[{nrows}x{ncols} SparseFrame of type '<class " \
"'{dtype}'>' \n with {nnz} stored elements " \
"in Compressed Sparse Row format]".format(
nrows=self.shape[0],
ncols=self.shape[1],
dtype=self.data.dtype,
nnz=self.data.nnz
)
repr = "{data}\n{sparse}"\
.format(data='\n'.join(df_str),
sparse=sparse_str)
nrows=self.shape[0],
ncols=self.shape[1],
dtype=self.data.dtype,
nnz=self.data.nnz
)
repr = "{data}\n{sparse}" \
.format(data='\n'.join(df_str), sparse=sparse_str)
return repr

def __array__(self):
Expand Down
11 changes: 11 additions & 0 deletions sparsity/test/test_sparse_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,22 @@ def test_drop_duplicate_idx():


def test_repr():
sf = SparseFrame(sparse.csr_matrix((2, 3)))
res = sf.__repr__()
assert isinstance(res, str)
assert len(res.splitlines()) == 1 + 2 + 2 # column names + 2 rows + descr.

sf = SparseFrame(sparse.csr_matrix((10, 10000)))
res = sf.__repr__()
assert isinstance(res, str)
assert '10x10000' in res
assert '0 stored' in res
assert len(res.splitlines()) == 1 + 5 + 2

sf = SparseFrame(sparse.csr_matrix((10000, 10000)))
res = sf.__repr__()
assert isinstance(res, str)
assert len(res.splitlines()) == 1 + 5 + 2

sf = SparseFrame(np.array([]), index=[], columns=['A', 'B'])
res = sf.__repr__()
Expand Down