Skip to content

Commit

Permalink
ENH: density property for SparseSeries #2384
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Nov 29, 2012
1 parent 225e4d2 commit 95500f0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pandas 0.10.0
for both Series and DataFrame (#2002)
- Add ``duplicated`` and ``drop_duplicates`` functions to Series (#1923)
- Add docs for ``HDFStore table`` format
- 'density' property in `SparseSeries` (#2384)

**API Changes**

Expand Down
9 changes: 5 additions & 4 deletions doc/source/v0.10.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Updated PyTables Support
store = HDFStore('store.h5')
df = DataFrame(randn(8, 3), index=date_range('1/1/2000', periods=8),
columns=['A', 'B', 'C'])
df
df

# appending data frames
df1 = df[0:4]
Expand Down Expand Up @@ -66,17 +66,18 @@ Updated PyTables Support
- added mixed-dtype support!

.. ipython:: python

df['string'] = 'string'
df['int'] = 1

store.append('df',df)
df1 = store.select('df')
df1 = store.select('df')
df1
df1.get_dtype_counts()

- performance improvments on table writing
- support for arbitrarily indexed dimensions
- ``SparseSeries`` now has a ``density`` property (#2384)

**Bug Fixes**

Expand Down
4 changes: 4 additions & 0 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ def to_dense(self, sparse_only=False):
else:
return Series(self.values, index=self.index, name=self.name)

@property
def density(self):
return float(len(self.sp_index)) / len(self.index)

def astype(self, dtype=None):
"""
Expand Down
4 changes: 4 additions & 0 deletions pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ def test_dense_to_sparse(self):
self.assertEquals(sdf.default_fill_value, 0)
tm.assert_frame_equal(sdf.to_dense(), df)

def test_density(self):
df = SparseSeries([nan, nan, nan, 0, 1, 2, 3, 4, 5, 6])
self.assertEquals(df.density, 0.7)

def test_sparse_to_dense(self):
pass

Expand Down

0 comments on commit 95500f0

Please sign in to comment.