Skip to content

Commit 0570237

Browse files
authored
slightly clean info.py ahead of adding Series.info (#33625)
1 parent 4574fa5 commit 0570237

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

pandas/io/formats/info.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,18 @@ def info(
158158
lines.append(str(type(data)))
159159
lines.append(data.index._summary())
160160

161-
if len(data.columns) == 0:
161+
cols = data.columns
162+
col_count = len(cols)
163+
dtypes = data.dtypes
164+
165+
if col_count == 0:
162166
lines.append(f"Empty {type(data).__name__}")
163167
fmt.buffer_put_lines(buf, lines)
164168
return
165169

166-
cols = data.columns
167-
col_count = len(data.columns)
168-
169170
# hack
170171
if max_cols is None:
171-
max_cols = get_option("display.max_info_columns", len(data.columns) + 1)
172+
max_cols = get_option("display.max_info_columns", col_count + 1)
172173

173174
max_rows = get_option("display.max_info_rows", len(data) + 1)
174175

@@ -179,7 +180,7 @@ def info(
179180
exceeds_info_cols = col_count > max_cols
180181

181182
def _verbose_repr():
182-
lines.append(f"Data columns (total {len(data.columns)} columns):")
183+
lines.append(f"Data columns (total {col_count} columns):")
183184

184185
id_head = " # "
185186
column_head = "Column"
@@ -196,9 +197,9 @@ def _verbose_repr():
196197
header = _put_str(id_head, space_num) + _put_str(column_head, space)
197198
if show_counts:
198199
counts = data.count()
199-
if len(cols) != len(counts): # pragma: no cover
200+
if col_count != len(counts): # pragma: no cover
200201
raise AssertionError(
201-
f"Columns must equal counts ({len(cols)} != {len(counts)})"
202+
f"Columns must equal counts ({col_count} != {len(counts)})"
202203
)
203204
count_header = "Non-Null Count"
204205
len_count = len(count_header)
@@ -214,7 +215,7 @@ def _verbose_repr():
214215

215216
dtype_header = "Dtype"
216217
len_dtype = len(dtype_header)
217-
max_dtypes = max(len(pprint_thing(k)) for k in data.dtypes)
218+
max_dtypes = max(len(pprint_thing(k)) for k in dtypes)
218219
space_dtype = max(len_dtype, max_dtypes)
219220
header += _put_str(count_header, space_count) + _put_str(
220221
dtype_header, space_dtype
@@ -228,14 +229,14 @@ def _verbose_repr():
228229
+ _put_str("-" * len_dtype, space_dtype)
229230
)
230231

231-
for i, col in enumerate(data.columns):
232-
dtype = data.dtypes.iloc[i]
232+
for i, col in enumerate(cols):
233+
dtype = dtypes[i]
233234
col = pprint_thing(col)
234235

235236
line_no = _put_str(f" {i}", space_num)
236237
count = ""
237238
if show_counts:
238-
count = counts.iloc[i]
239+
count = counts[i]
239240

240241
lines.append(
241242
line_no
@@ -245,7 +246,7 @@ def _verbose_repr():
245246
)
246247

247248
def _non_verbose_repr():
248-
lines.append(data.columns._summary(name="Columns"))
249+
lines.append(cols._summary(name="Columns"))
249250

250251
def _sizeof_fmt(num, size_qualifier):
251252
# returns size in human readable format
@@ -266,7 +267,7 @@ def _sizeof_fmt(num, size_qualifier):
266267
_verbose_repr()
267268

268269
# groupby dtype.name to collect e.g. Categorical columns
269-
counts = data.dtypes.value_counts().groupby(lambda x: x.name).sum()
270+
counts = dtypes.value_counts().groupby(lambda x: x.name).sum()
270271
dtypes = [f"{k[0]}({k[1]:d})" for k in sorted(counts.items())]
271272
lines.append(f"dtypes: {', '.join(dtypes)}")
272273

0 commit comments

Comments
 (0)