Skip to content

Commit 67218b7

Browse files
reidy-preidy-p
authored and
reidy-p
committed
BUG: Maintain column order with groupby.nth
1 parent ee80803 commit 67218b7

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ Groupby/Resample/Rolling
824824
- Bug in :meth:`SeriesGroupBy.mean` when values were integral but could not fit inside of int64, overflowing instead. (:issue:`22487`)
825825
- :func:`RollingGroupby.agg` and :func:`ExpandingGroupby.agg` now support multiple aggregation functions as parameters (:issue:`15072`)
826826
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` when resampling by a weekly offset (``'W'``) across a DST transition (:issue:`9119`, :issue:`21459`)
827+
- Bug in :func:`pandas.core.groupby.GroupBy.nth` where column order was not always preserved (:issue:`20760`)
827828

828829
Sparse
829830
^^^^^^

pandas/core/groupby/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ def _set_group_selection(self):
497497

498498
if len(groupers):
499499
# GH12839 clear selected obj cache when group selection changes
500-
self._group_selection = ax.difference(Index(groupers)).tolist()
500+
self._group_selection = ax.difference(Index(groupers),
501+
sort=False).tolist()
501502
self._reset_cache('_selected_obj')
502503

503504
def _set_result_index_ordered(self, result):

pandas/core/indexes/base.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -2903,17 +2903,20 @@ def intersection(self, other):
29032903
taken.name = None
29042904
return taken
29052905

2906-
def difference(self, other):
2906+
def difference(self, other, sort=True):
29072907
"""
29082908
Return a new Index with elements from the index that are not in
29092909
`other`.
29102910
29112911
This is the set difference of two Index objects.
2912-
It's sorted if sorting is possible.
29132912
29142913
Parameters
29152914
----------
29162915
other : Index or array-like
2916+
sort : bool, default True
2917+
Sort the resulting index if possible
2918+
2919+
.. versionadded:: 0.24.0
29172920
29182921
Returns
29192922
-------
@@ -2922,10 +2925,12 @@ def difference(self, other):
29222925
Examples
29232926
--------
29242927
2925-
>>> idx1 = pd.Index([1, 2, 3, 4])
2928+
>>> idx1 = pd.Index([2, 1, 3, 4])
29262929
>>> idx2 = pd.Index([3, 4, 5, 6])
29272930
>>> idx1.difference(idx2)
29282931
Int64Index([1, 2], dtype='int64')
2932+
>>> idx1.difference(idx2, sort=False)
2933+
Int64Index([2, 1], dtype='int64')
29292934
29302935
"""
29312936
self._assert_can_do_setop(other)
@@ -2943,10 +2948,11 @@ def difference(self, other):
29432948
label_diff = np.setdiff1d(np.arange(this.size), indexer,
29442949
assume_unique=True)
29452950
the_diff = this.values.take(label_diff)
2946-
try:
2947-
the_diff = sorting.safe_sort(the_diff)
2948-
except TypeError:
2949-
pass
2951+
if sort:
2952+
try:
2953+
the_diff = sorting.safe_sort(the_diff)
2954+
except TypeError:
2955+
pass
29502956

29512957
return this._shallow_copy(the_diff, name=result_name, freq=None)
29522958

pandas/tests/groupby/test_nth.py

+24
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,27 @@ def test_nth_empty():
390390
names=['a', 'b']),
391391
columns=['c'])
392392
assert_frame_equal(result, expected)
393+
394+
395+
def test_nth_column_order():
396+
# GH 20760
397+
# Check that nth preserves column order
398+
df = DataFrame([[1, 'b', 100],
399+
[1, 'a', 50],
400+
[1, 'a', np.nan],
401+
[2, 'c', 200],
402+
[2, 'd', 150]],
403+
columns=['A', 'C', 'B'])
404+
result = df.groupby('A').nth(0)
405+
expected = DataFrame([['b', 100.0],
406+
['c', 200.0]],
407+
columns=['C', 'B'],
408+
index=Index([1, 2], name='A'))
409+
assert_frame_equal(result, expected)
410+
411+
result = df.groupby('A').nth(-1, dropna='any')
412+
expected = DataFrame([['a', 50.0],
413+
['d', 150.0]],
414+
columns=['C', 'B'],
415+
index=Index([1, 2], name='A'))
416+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)