Skip to content

Commit 321db90

Browse files
committed
add examples
1 parent 0f6e2c3 commit 321db90

File tree

3 files changed

+103
-82
lines changed

3 files changed

+103
-82
lines changed

pandas/core/frame.py

+83
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,89 @@ def to_html(
22322232
is used. By default, the setting in
22332233
``pandas.options.display.max_info_columns`` is used.
22342234
""",
2235+
examples_sub="""
2236+
>>> int_values = [1, 2, 3, 4, 5]
2237+
>>> text_values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']
2238+
>>> float_values = [0.0, 0.25, 0.5, 0.75, 1.0]
2239+
>>> df = pd.DataFrame({"int_col": int_values, "text_col": text_values,
2240+
... "float_col": float_values})
2241+
>>> df
2242+
int_col text_col float_col
2243+
0 1 alpha 0.00
2244+
1 2 beta 0.25
2245+
2 3 gamma 0.50
2246+
3 4 delta 0.75
2247+
4 5 epsilon 1.00
2248+
2249+
Prints information of all columns:
2250+
2251+
>>> df.info(verbose=True)
2252+
<class 'pandas.core.frame.DataFrame'>
2253+
RangeIndex: 5 entries, 0 to 4
2254+
Data columns (total 3 columns):
2255+
# Column Non-Null Count Dtype
2256+
--- ------ -------------- -----
2257+
0 int_col 5 non-null int64
2258+
1 text_col 5 non-null object
2259+
2 float_col 5 non-null float64
2260+
dtypes: float64(1), int64(1), object(1)
2261+
memory usage: 248.0+ bytes
2262+
2263+
Prints a summary of columns count and its dtypes but not per column
2264+
information:
2265+
2266+
>>> df.info(verbose=False)
2267+
<class 'pandas.core.frame.DataFrame'>
2268+
RangeIndex: 5 entries, 0 to 4
2269+
Columns: 3 entries, int_col to float_col
2270+
dtypes: float64(1), int64(1), object(1)
2271+
memory usage: 248.0+ bytes
2272+
2273+
Pipe output of DataFrame.info to buffer instead of sys.stdout, get
2274+
buffer content and writes to a text file:
2275+
2276+
>>> import io
2277+
>>> buffer = io.StringIO()
2278+
>>> df.info(buf=buffer)
2279+
>>> s = buffer.getvalue()
2280+
>>> with open("df_info.txt", "w",
2281+
... encoding="utf-8") as f: # doctest: +SKIP
2282+
... f.write(s)
2283+
260
2284+
2285+
The `memory_usage` parameter allows deep introspection mode, specially
2286+
useful for big DataFrames and fine-tune memory optimization:
2287+
2288+
>>> random_strings_array = np.random.choice(['a', 'b', 'c'], 10 ** 6)
2289+
>>> df = pd.DataFrame({
2290+
... 'column_1': np.random.choice(['a', 'b', 'c'], 10 ** 6),
2291+
... 'column_2': np.random.choice(['a', 'b', 'c'], 10 ** 6),
2292+
... 'column_3': np.random.choice(['a', 'b', 'c'], 10 ** 6)
2293+
... })
2294+
>>> df.info()
2295+
<class 'pandas.core.frame.DataFrame'>
2296+
RangeIndex: 1000000 entries, 0 to 999999
2297+
Data columns (total 3 columns):
2298+
# Column Non-Null Count Dtype
2299+
--- ------ -------------- -----
2300+
0 column_1 1000000 non-null object
2301+
1 column_2 1000000 non-null object
2302+
2 column_3 1000000 non-null object
2303+
dtypes: object(3)
2304+
memory usage: 22.9+ MB
2305+
2306+
>>> df.info(memory_usage='deep')
2307+
<class 'pandas.core.frame.DataFrame'>
2308+
RangeIndex: 1000000 entries, 0 to 999999
2309+
Data columns (total 3 columns):
2310+
# Column Non-Null Count Dtype
2311+
--- ------ -------------- -----
2312+
0 column_1 1000000 non-null object
2313+
1 column_2 1000000 non-null object
2314+
2 column_3 1000000 non-null object
2315+
dtypes: object(3)
2316+
memory usage: 188.8 MB
2317+
""",
22352318
)
22362319
@Appender(NDFrame.info.__doc__)
22372320
def info(

pandas/core/generic.py

+1-81
Original file line numberDiff line numberDiff line change
@@ -1792,87 +1792,7 @@ def info(
17921792
17931793
Examples
17941794
--------
1795-
>>> int_values = [1, 2, 3, 4, 5]
1796-
>>> text_values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']
1797-
>>> float_values = [0.0, 0.25, 0.5, 0.75, 1.0]
1798-
>>> df = pd.DataFrame({"int_col": int_values, "text_col": text_values,
1799-
... "float_col": float_values})
1800-
>>> df
1801-
int_col text_col float_col
1802-
0 1 alpha 0.00
1803-
1 2 beta 0.25
1804-
2 3 gamma 0.50
1805-
3 4 delta 0.75
1806-
4 5 epsilon 1.00
1807-
1808-
Prints information of all columns:
1809-
1810-
>>> df.info(verbose=True)
1811-
<class 'pandas.core.frame.DataFrame'>
1812-
RangeIndex: 5 entries, 0 to 4
1813-
Data columns (total 3 columns):
1814-
# Column Non-Null Count Dtype
1815-
--- ------ -------------- -----
1816-
0 int_col 5 non-null int64
1817-
1 text_col 5 non-null object
1818-
2 float_col 5 non-null float64
1819-
dtypes: float64(1), int64(1), object(1)
1820-
memory usage: 248.0+ bytes
1821-
1822-
Prints a summary of columns count and its dtypes but not per column
1823-
information:
1824-
1825-
>>> df.info(verbose=False)
1826-
<class 'pandas.core.frame.DataFrame'>
1827-
RangeIndex: 5 entries, 0 to 4
1828-
Columns: 3 entries, int_col to float_col
1829-
dtypes: float64(1), int64(1), object(1)
1830-
memory usage: 248.0+ bytes
1831-
1832-
Pipe output of DataFrame.info to buffer instead of sys.stdout, get
1833-
buffer content and writes to a text file:
1834-
1835-
>>> import io
1836-
>>> buffer = io.StringIO()
1837-
>>> df.info(buf=buffer)
1838-
>>> s = buffer.getvalue()
1839-
>>> with open("df_info.txt", "w",
1840-
... encoding="utf-8") as f: # doctest: +SKIP
1841-
... f.write(s)
1842-
260
1843-
1844-
The `memory_usage` parameter allows deep introspection mode, specially
1845-
useful for big DataFrames and fine-tune memory optimization:
1846-
1847-
>>> random_strings_array = np.random.choice(['a', 'b', 'c'], 10 ** 6)
1848-
>>> df = pd.DataFrame({
1849-
... 'column_1': np.random.choice(['a', 'b', 'c'], 10 ** 6),
1850-
... 'column_2': np.random.choice(['a', 'b', 'c'], 10 ** 6),
1851-
... 'column_3': np.random.choice(['a', 'b', 'c'], 10 ** 6)
1852-
... })
1853-
>>> df.info()
1854-
<class 'pandas.core.frame.DataFrame'>
1855-
RangeIndex: 1000000 entries, 0 to 999999
1856-
Data columns (total 3 columns):
1857-
# Column Non-Null Count Dtype
1858-
--- ------ -------------- -----
1859-
0 column_1 1000000 non-null object
1860-
1 column_2 1000000 non-null object
1861-
2 column_3 1000000 non-null object
1862-
dtypes: object(3)
1863-
memory usage: 22.9+ MB
1864-
1865-
>>> df.info(memory_usage='deep')
1866-
<class 'pandas.core.frame.DataFrame'>
1867-
RangeIndex: 1000000 entries, 0 to 999999
1868-
Data columns (total 3 columns):
1869-
# Column Non-Null Count Dtype
1870-
--- ------ -------------- -----
1871-
0 column_1 1000000 non-null object
1872-
1 column_2 1000000 non-null object
1873-
2 column_3 1000000 non-null object
1874-
dtypes: object(3)
1875-
memory usage: 188.8 MB
1795+
%(examples_sub)s
18761796
"""
18771797
if buf is None: # pragma: no cover
18781798
buf = sys.stdout

pandas/core/series.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -4182,7 +4182,25 @@ def replace(
41824182
method=method,
41834183
)
41844184

4185-
@Substitution(klass="Series", type_sub="", max_cols_sub="")
4185+
@Substitution(
4186+
klass="Series",
4187+
type_sub="",
4188+
max_cols_sub="",
4189+
examples_sub="""
4190+
>>> int_values = [1, 2, 3, 4, 5]
4191+
>>> text_values = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']
4192+
>>> s = pd.Series(text_values, index=int_values)
4193+
>>> s.info()
4194+
<class 'pandas.core.series.Series'>
4195+
Int64Index: 5 entries, 1 to 5
4196+
Series name: None
4197+
# Non-Null Count Dtype
4198+
--- -------------- -----
4199+
0 5 non-null object
4200+
dtypes: object(1)
4201+
memory usage: 80.0+ bytes
4202+
""",
4203+
)
41864204
@Appender(NDFrame.info.__doc__)
41874205
def info(
41884206
self, verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None

0 commit comments

Comments
 (0)