Skip to content

Commit 3246267

Browse files
author
y-p
committed
Merge pull request #5682 from cpcloud/display-column-dtypes
DISP: show column dtype in DataFrame.info() output
2 parents 5e176a9 + 8514688 commit 3246267

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1427,10 +1427,12 @@ def info(self, verbose=True, buf=None, max_cols=None):
14271427
if len(cols) != len(counts): # pragma: no cover
14281428
raise AssertionError('Columns must equal counts (%d != %d)' %
14291429
(len(cols), len(counts)))
1430+
dtypes = self.dtypes
14301431
for col, count in compat.iteritems(counts):
1432+
dtype = dtypes[col]
14311433
col = com.pprint_thing(col)
14321434
lines.append(_put_str(col, space) +
1433-
'%d non-null values' % count)
1435+
'%d non-null %s' % (count, dtype))
14341436
else:
14351437
lines.append(self.columns.summary(name='Columns'))
14361438

pandas/tests/test_frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -6018,6 +6018,21 @@ def test_info_duplicate_columns(self):
60186018
columns=['a', 'a', 'b', 'b'])
60196019
frame.info(buf=io)
60206020

6021+
def test_info_shows_column_dtypes(self):
6022+
dtypes = ['int64', 'float64', 'datetime64[ns]', 'timedelta64[ns]',
6023+
'complex128', 'object', 'bool']
6024+
data = {}
6025+
n = 10
6026+
for i, dtype in enumerate(dtypes):
6027+
data[i] = np.random.randint(2, size=n).astype(dtype)
6028+
df = DataFrame(data)
6029+
buf = StringIO()
6030+
df.info(buf=buf)
6031+
res = buf.getvalue()
6032+
for i, dtype in enumerate(dtypes):
6033+
name = '%d %d non-null %s' % (i, n, dtype)
6034+
assert name in res
6035+
60216036
def test_dtypes(self):
60226037
self.mixed_frame['bool'] = self.mixed_frame['A'] > 0
60236038
result = self.mixed_frame.dtypes

pandas/tests/test_series.py

+6
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,12 @@ def test_fillna_raise(self):
27412741
self.assertRaises(TypeError, s.fillna, [1, 2])
27422742
self.assertRaises(TypeError, s.fillna, (1, 2))
27432743

2744+
def test_raise_on_info(self):
2745+
s = Series(np.random.randn(10))
2746+
with tm.assertRaises(AttributeError):
2747+
s.info()
2748+
2749+
27442750
# TimeSeries-specific
27452751

27462752
def test_fillna(self):

0 commit comments

Comments
 (0)