From 56beebfb2439917e08816246e7dff5fb04985e92 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 15 May 2018 09:53:36 -0500 Subject: [PATCH 1/2] Fixed space in small info Closes https://github.com/pandas-dev/pandas/issues/21056 --- pandas/core/frame.py | 2 +- pandas/tests/frame/test_repr_info.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0437c479c9d81..dccc840f5affd 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2231,7 +2231,7 @@ def _sizeof_fmt(num, size_qualifier): # returns size in human readable format for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: if num < 1024.0: - return ("{num:3.1f}{size_q}" + return ("{num:3.1f}{size_q} " "{x}".format(num=num, size_q=size_qualifier, x=x)) num /= 1024.0 return "{num:3.1f}{size_q} {pb}".format(num=num, diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index 8fc6fef11798a..855d311d8c8de 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -5,6 +5,7 @@ from datetime import datetime, timedelta import re import sys +import textwrap from numpy import nan import numpy as np @@ -204,6 +205,24 @@ def test_info(self): frame.info() frame.info(verbose=False) + def test_info_memory(self): + # https://github.com/pandas-dev/pandas/issues/21056 + df = pd.DataFrame({'a': pd.Series([1, 2], dtype='i8')}) + buf = StringIO() + df.info(buf=buf) + result = buf.getvalue() + + expected = textwrap.dedent("""\ + + RangeIndex: 2 entries, 0 to 1 + Data columns (total 1 columns): + a 2 non-null int64 + dtypes: int64(1) + memory usage: 96.0 bytes + """) + + assert result == expected + def test_info_wide(self): from pandas import set_option, reset_option io = StringIO() From 2297fee2f0163c9ee60cc85112baf770e1bd8610 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 15 May 2018 10:52:23 -0500 Subject: [PATCH 2/2] compute memory usage --- pandas/tests/frame/test_repr_info.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index 855d311d8c8de..668613c494a47 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -211,6 +211,7 @@ def test_info_memory(self): buf = StringIO() df.info(buf=buf) result = buf.getvalue() + bytes = float(df.memory_usage().sum()) expected = textwrap.dedent("""\ @@ -218,8 +219,8 @@ def test_info_memory(self): Data columns (total 1 columns): a 2 non-null int64 dtypes: int64(1) - memory usage: 96.0 bytes - """) + memory usage: {} bytes + """.format(bytes)) assert result == expected