1
1
from abc import ABCMeta , abstractmethod
2
2
import sys
3
- from typing import IO , TYPE_CHECKING , List , Optional , Tuple , Union , cast
3
+ from typing import IO , TYPE_CHECKING , List , NamedTuple , Optional , Tuple , Union , cast
4
4
5
5
from pandas ._config import get_option
6
6
15
15
from pandas .core .series import Series # noqa: F401
16
16
17
17
18
+ class CountConfigs (NamedTuple ):
19
+ """
20
+ Configs with which to display counts.
21
+
22
+ Attributes
23
+ ----------
24
+ counts : Series
25
+ Non-null count of Series (or of each column of DataFrame).
26
+ count_header : str
27
+ Header that will be printed out above non-null counts in output.
28
+ space_count : int
29
+ Number of spaces that count_header should occupy
30
+ (including space before `dtypes` column).
31
+ len_count : int
32
+ Length of count header.
33
+ count_temp : str
34
+ String that can be formatted to include non-null count.
35
+ """
36
+
37
+ counts : "Series"
38
+ count_header : str
39
+ space_count : int
40
+ len_count : int
41
+ count_temp : str
42
+
43
+
44
+ class HeaderAndSpaceConfigs (NamedTuple ):
45
+ """
46
+ Attributes
47
+ ----------
48
+ space_dtype : int
49
+ Number of spaces that `dtypes` column should occupy.
50
+ header : str
51
+ Header with extra columns (count and type) appended.
52
+ len_dtype : int
53
+ Length of dtype header.
54
+ """
55
+
56
+ space_dtype : int
57
+ header : str
58
+ len_dtype : int
59
+
60
+
18
61
def _put_str (s : Union [str , Dtype ], space : int ) -> str :
19
62
"""
20
63
Make string of specified length, padding to the right if necessary.
@@ -74,7 +117,7 @@ def _sizeof_fmt(num: Union[int, float], size_qualifier: str) -> str:
74
117
75
118
def _get_count_configs (
76
119
counts : "Series" , col_space : int , show_counts : bool , col_count : Optional [int ] = None
77
- ) -> Tuple [ str , int , int , str ] :
120
+ ) -> CountConfigs :
78
121
"""
79
122
Get configs for displaying counts, depending on the value of `show_counts`.
80
123
@@ -91,15 +134,7 @@ def _get_count_configs(
91
134
92
135
Returns
93
136
-------
94
- count_header : str
95
- Header that will be printed out above non-null counts in output.
96
- space_count : int
97
- Number of spaces that count_header should occupy
98
- (including space before `dtypes` column).
99
- len_count : int
100
- Length of count header.
101
- count_temp : str
102
- String that can be formatted to include non-null count.
137
+ CountConfigs
103
138
"""
104
139
if show_counts :
105
140
if col_count is not None and col_count != len (counts ): # pragma: no cover
@@ -117,17 +152,15 @@ def _get_count_configs(
117
152
space_count = len (count_header )
118
153
len_count = space_count
119
154
count_temp = "{count}"
120
- return count_header , space_count , len_count , count_temp
155
+ return CountConfigs ( counts , count_header , space_count , len_count , count_temp )
121
156
122
157
123
158
def _display_counts_and_dtypes (
124
159
lines : List [str ],
125
160
ids : "Index" ,
126
161
dtypes : "Series" ,
127
162
show_counts : bool ,
128
- counts : "Series" ,
129
- count_temp : str ,
130
- space_count : int ,
163
+ count_configs : CountConfigs ,
131
164
space_dtype : int ,
132
165
space : int = 0 ,
133
166
space_num : int = 0 ,
@@ -145,13 +178,8 @@ def _display_counts_and_dtypes(
145
178
Series dtype (or dtypes of DataFrame columns).
146
179
show_counts : bool
147
180
Whether to show non-null counts.
148
- counts : Series
149
- Non-null counts of Series (or of each of DataFrame's columns).
150
- count_temp : str
151
- String that can be formatted to include non-null count.
152
- space_count : int
153
- Number of spaces that count_header should occupy
154
- (including space before `dtypes` column).
181
+ count_configs: CountConfigs
182
+ Configs with which to display counts.
155
183
space_dtype : int
156
184
Number of spaces that `dtypes` column should occupy.
157
185
space : int = 0
@@ -168,12 +196,14 @@ def _display_counts_and_dtypes(
168
196
line_no = _put_str (f" { i } " , space_num )
169
197
count = ""
170
198
if show_counts :
171
- count = counts [i ]
199
+ count = count_configs . counts [i ]
172
200
173
201
lines .append (
174
202
line_no
175
203
+ _put_str (col , space )
176
- + _put_str (count_temp .format (count = count ), space_count )
204
+ + _put_str (
205
+ count_configs .count_temp .format (count = count ), count_configs .space_count
206
+ )
177
207
+ _put_str (dtype , space_dtype )
178
208
)
179
209
@@ -439,30 +469,26 @@ def _verbose_repr(
439
469
440
470
header = _put_str (id_head , space_num ) + _put_str (column_head , space )
441
471
counts = self .data .count ()
442
- count_header , space_count , len_count , count_temp = _get_count_configs (
443
- counts , col_space , show_counts , col_count
444
- )
472
+ count_configs = _get_count_configs (counts , col_space , show_counts , col_count )
445
473
446
474
space_dtype , header , len_dtype = _get_header_and_spaces (
447
- dtypes , space_count , count_header , header
475
+ dtypes , count_configs . space_count , count_configs . count_header , header
448
476
)
449
477
450
478
lines .append (header )
451
479
lines .append (
452
480
_put_str ("-" * len_id , space_num )
453
481
+ _put_str ("-" * len_column , space )
454
- + _put_str ("-" * len_count , space_count )
455
- + _put_str ("-" * len_dtype , space_dtype )
482
+ + _put_str ("-" * count_configs . len_count , count_configs . space_count )
483
+ + _put_str ("-" * len_dtype , space_dtype , )
456
484
)
457
485
458
486
_display_counts_and_dtypes (
459
487
lines ,
460
488
ids ,
461
489
dtypes ,
462
490
show_counts ,
463
- counts ,
464
- count_temp ,
465
- space_count ,
491
+ count_configs ,
466
492
space_dtype ,
467
493
space ,
468
494
space_num ,
@@ -489,29 +515,20 @@ def _verbose_repr(
489
515
id_space = 2
490
516
491
517
counts = cast ("Series" , self .data ._constructor (self .data .count ()))
492
- count_header , space_count , len_count , count_temp = _get_count_configs (
493
- counts , id_space , show_counts
494
- )
518
+ count_configs = _get_count_configs (counts , id_space , show_counts )
495
519
496
520
space_dtype , header , len_dtype = _get_header_and_spaces (
497
- dtypes , space_count , count_header
521
+ dtypes , count_configs . space_count , count_configs . count_header
498
522
)
499
523
500
524
lines .append (header )
501
525
lines .append (
502
- _put_str ("-" * len_count , space_count )
526
+ _put_str ("-" * count_configs . len_count , count_configs . space_count )
503
527
+ _put_str ("-" * len_dtype , space_dtype )
504
528
)
505
529
506
530
_display_counts_and_dtypes (
507
- lines ,
508
- ids ,
509
- dtypes ,
510
- show_counts ,
511
- counts ,
512
- count_temp ,
513
- space_count ,
514
- space_dtype ,
531
+ lines , ids , dtypes , show_counts , count_configs , space_dtype ,
515
532
)
516
533
517
534
def _non_verbose_repr (self , lines : List [str ], ids : "Index" ) -> None :
0 commit comments