Skip to content

Commit ecdab30

Browse files
committed
update examples for series.info
1 parent 8e6ad16 commit ecdab30

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

pandas/core/series.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -4198,7 +4198,52 @@ def replace(
41984198
--- -------------- -----
41994199
0 5 non-null object
42004200
dtypes: object(1)
4201-
memory usage: 80.0+ bytes""",
4201+
memory usage: 80.0+ bytes
4202+
4203+
Prints a summary excluding information about its values:
4204+
4205+
>>> s.info(verbose=False)
4206+
<class 'pandas.core.series.Series'>
4207+
Int64Index: 5 entries, 1 to 5
4208+
dtypes: object(1)
4209+
memory usage: 80.0+ bytes
4210+
4211+
Pipe output of DataFrame.info to buffer instead of sys.stdout, get
4212+
buffer content and writes to a text file:
4213+
4214+
>>> import io
4215+
>>> buffer = io.StringIO()
4216+
>>> s.info(buf=buffer)
4217+
>>> s = buffer.getvalue()
4218+
>>> with open("df_info.txt", "w",
4219+
... encoding="utf-8") as f: # doctest: +SKIP
4220+
... f.write(s)
4221+
260
4222+
4223+
The `memory_usage` parameter allows deep introspection mode, specially
4224+
useful for big Series and fine-tune memory optimization:
4225+
4226+
>>> random_strings_array = np.random.choice(['a', 'b', 'c'], 10 ** 6)
4227+
>>> s = pd.Series(np.random.choice(['a', 'b', 'c'], 10 ** 6))
4228+
>>> s.info()
4229+
<class 'pandas.core.series.Series'>
4230+
RangeIndex: 1000000 entries, 0 to 999999
4231+
Series name: None
4232+
# Non-Null Count Dtype
4233+
--- -------------- -----
4234+
0 1000000 non-null object
4235+
dtypes: object(1)
4236+
memory usage: 7.6+ MB
4237+
4238+
>>> s.info(memory_usage='deep')
4239+
<class 'pandas.core.series.Series'>
4240+
RangeIndex: 1000000 entries, 0 to 999999
4241+
Series name: None
4242+
# Non-Null Count Dtype
4243+
--- -------------- -----
4244+
0 1000000 non-null object
4245+
dtypes: object(1)
4246+
memory usage: 62.9 MB""",
42024247
see_also_sub="""
42034248
Series.describe: Generate descriptive statistics of Series.
42044249
Series.memory_usage: Memory usage of Series.""",

pandas/tests/series/test_repr.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,15 @@ def test_categorical_series_repr_timedelta_ordered(self):
492492
assert repr(s) == exp
493493

494494
@pytest.mark.parametrize("verbose", [True, False])
495-
def test_info(self, verbose, capsys):
495+
def test_info(self, verbose):
496496
index = MultiIndex(
497497
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
498498
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
499499
names=["first", "second"],
500500
)
501501
s = Series(range(len(index)), index=index, name="sth")
502-
s.info(verbose=verbose)
502+
buf = StringIO()
503+
s.info(verbose=verbose, buf=buf)
503504
expected = """<class 'pandas.core.series.Series'>
504505
MultiIndex: 10 entries, ('foo', 'one') to ('qux', 'three')
505506
"""
@@ -512,7 +513,7 @@ def test_info(self, verbose, capsys):
512513
expected += f"""dtypes: int64(1)
513514
memory usage: {s.memory_usage()}.0+ bytes
514515
"""
515-
result = capsys.readouterr().out
516+
result = buf.getvalue()
516517
assert result == expected
517518

518519
@pytest.mark.skipif(PYPY, reason="on PyPy deep=True doesn't change result")

0 commit comments

Comments
 (0)