Skip to content

Commit 33142cb

Browse files
committed
Move truncated dict repr to Index.groupby()
1 parent 3d4b057 commit 33142cb

File tree

6 files changed

+44
-43
lines changed

6 files changed

+44
-43
lines changed

pandas/core/groupby/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from pandas.core.dtypes.common import is_list_like, is_scalar
1212

13-
1413
class GroupByMixin(object):
1514
"""
1615
Provide the groupby facilities to the mixed object.

pandas/core/groupby/groupby.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def groups(self):
390390
Dict {group name -> group labels}.
391391
"""
392392
self._assure_grouper()
393-
return DataFrameGroups(self.grouper.groups)
393+
return self.grouper.groups
394394

395395
@property
396396
def ngroups(self):
@@ -2204,25 +2204,3 @@ def groupby(obj, by, **kwds):
22042204
raise TypeError('invalid type: {}'.format(obj))
22052205

22062206
return klass(obj, by, **kwds)
2207-
2208-
2209-
class DataFrameGroups(dict):
2210-
def __repr__(self):
2211-
from pandas.compat import u
2212-
2213-
nitems = get_option('display.max_rows') or len(self)
2214-
2215-
fmt = u("{{{things}}}")
2216-
pfmt = u("{key}: {val}")
2217-
2218-
pairs = []
2219-
for k, v in list(self.items()):
2220-
pairs.append(pfmt.format(key=k, val=v))
2221-
2222-
if nitems < len(self):
2223-
start_cnt, end_cnt = nitems - int(nitems / 2), int(nitems / 2)
2224-
return fmt.format(things=", ".join(pairs[:start_cnt]) +
2225-
", ... , " +
2226-
", ".join(pairs[-end_cnt:]))
2227-
else:
2228-
return fmt.format(things=", ".join(pairs))

pandas/core/groupby/grouper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ class Grouping(object):
236236

237237
def __init__(self, index, grouper=None, obj=None, name=None, level=None,
238238
sort=True, observed=False, in_axis=False):
239-
240239
self.name = name
241240
self.level = level
242241
self.grouper = _convert_grouper(index, grouper)

pandas/core/groupby/ops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,13 @@ def size(self):
235235
@cache_readonly
236236
def groups(self):
237237
""" dict {group name -> group labels} """
238+
238239
if len(self.groupings) == 1:
239240
return self.groupings[0].groups
240241
else:
241242
to_groupby = lzip(*(ping.grouper for ping in self.groupings))
242243
to_groupby = Index(to_groupby)
243-
return self.axis.groupby(to_groupby)
244+
return BaseGrouperGroups(self.axis.groupby(to_groupby))
244245

245246
@cache_readonly
246247
def is_monotonic(self):
@@ -361,7 +362,7 @@ def get_group_levels(self):
361362

362363
def _is_builtin_func(self, arg):
363364
"""
364-
if we define an builtin function for this argument, return it,
365+
if we define a builtin function for this argument, return it,
365366
otherwise return the arg
366367
"""
367368
return SelectionMixin._builtin_table.get(arg, arg)

pandas/core/indexes/base.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from pandas.core.arrays import ExtensionArray
3939
from pandas.core.base import IndexOpsMixin, PandasObject
4040
import pandas.core.common as com
41+
from pandas.core.config import get_option
4142
from pandas.core.indexes.frozen import FrozenList
4243
import pandas.core.missing as missing
4344
from pandas.core.ops import get_op_result_name, make_invalid_op
@@ -4493,7 +4494,7 @@ def groupby(self, values):
44934494
# map to the label
44944495
result = {k: self.take(v) for k, v in compat.iteritems(result)}
44954496

4496-
return result
4497+
return BaseGroupbyGroups(result)
44974498

44984499
def map(self, mapper, na_action=None):
44994500
"""
@@ -5290,6 +5291,29 @@ def _add_logical_methods_disabled(cls):
52905291
Index._add_comparison_methods()
52915292

52925293

5294+
class IndexGroupbyGroups(dict):
5295+
def __repr__(self):
5296+
from pandas.compat import u
5297+
5298+
nitems = get_option('display.max_rows') or len(self)
5299+
5300+
fmt = u("{{{things}}}")
5301+
pfmt = u("{key}: {val}")
5302+
5303+
pairs = []
5304+
for k, v in list(self.items()):
5305+
pairs.append(pfmt.format(key=k, val=v))
5306+
5307+
if nitems < len(self):
5308+
print("Truncating repr")
5309+
start_cnt, end_cnt = nitems - int(nitems / 2), int(nitems / 2)
5310+
return fmt.format(things=", ".join(pairs[:start_cnt]) +
5311+
", ... , " +
5312+
", ".join(pairs[-end_cnt:]))
5313+
else:
5314+
return fmt.format(things=", ".join(pairs))
5315+
5316+
52935317
def ensure_index_from_sequences(sequences, names=None):
52945318
"""
52955319
Construct an index from sequences of data.

pandas/tests/groupby/test_grouping.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -667,21 +667,21 @@ def test_gb_key_len_equal_axis_len(self):
667667

668668
class TestIteration():
669669

670-
# def test_groups(self, df):
671-
# grouped = df.groupby(['A'])
672-
# groups = grouped.groups
673-
# assert groups is grouped.groups # caching works
674-
#
675-
# for k, v in compat.iteritems(grouped.groups):
676-
# assert (df.loc[v]['A'] == k).all()
677-
#
678-
# grouped = df.groupby(['A', 'B'])
679-
# groups = grouped.groups
680-
# assert groups is grouped.groups # caching works
681-
#
682-
# for k, v in compat.iteritems(grouped.groups):
683-
# assert (df.loc[v]['A'] == k[0]).all()
684-
# assert (df.loc[v]['B'] == k[1]).all()
670+
def test_groups(self, df):
671+
grouped = df.groupby(['A'])
672+
groups = grouped.groups
673+
assert groups is grouped.groups # caching works
674+
675+
for k, v in compat.iteritems(grouped.groups):
676+
assert (df.loc[v]['A'] == k).all()
677+
678+
grouped = df.groupby(['A', 'B'])
679+
groups = grouped.groups
680+
assert groups is grouped.groups # caching works
681+
682+
for k, v in compat.iteritems(grouped.groups):
683+
assert (df.loc[v]['A'] == k[0]).all()
684+
assert (df.loc[v]['B'] == k[1]).all()
685685

686686
def test_grouping_is_iterable(self, tsframe):
687687
# this code path isn't used anywhere else

0 commit comments

Comments
 (0)