Skip to content

Commit

Permalink
ENH: get_dtype_counts publicized and added dtypes property
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Sep 22, 2011
1 parent 9ae251b commit dca3c5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pandas 0.4.1

This is a bug fix release

**New features / modules**

- Added new `DataFrame` methods `get_dtype_counts` and property `dtypes`

**Bug fixes**

- Fixed DataFrame constructor bug causing downstream problems (e.g. .copy()
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,19 +648,23 @@ def info(self, verbose=True, buf=None):
% (_stringify(cols[0]),
_stringify(cols[-1])))

counts = self._get_dtype_counts()
counts = self.get_dtype_counts()
dtypes = ['%s(%d)' % k for k in sorted(counts.iteritems())]
buf.write(u'dtypes: %s' % ', '.join(dtypes))

def _get_dtype_counts(self):
@property
def dtypes(self):
return self.apply(lambda x: x.dtype)

def get_dtype_counts(self):
counts = {}
for _, series in self.iteritems():
if series.dtype in counts:
counts[series.dtype] += 1
else:
counts[series.dtype] = 1

return counts
return Series(counts)

#----------------------------------------------------------------------
# properties for index and columns
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,12 @@ def test_info(self):
self.frame.info(buf=io)
self.tsframe.info(buf=io)

def test_dtypes(self):
self.mixed_frame['bool'] = self.mixed_frame['A'] > 0
result = self.mixed_frame.dtypes
expected = self.mixed_frame.dtypes
assert_series_equal(result, expected)

def test_append(self):
begin_index = self.frame.index[:5]
end_index = self.frame.index[5:]
Expand Down

0 comments on commit dca3c5c

Please sign in to comment.