Skip to content

Commit 997b963

Browse files
Chang Shewesm
Chang She
authored andcommitted
ENH: inplace option for sort_index
1 parent c6d8a31 commit 997b963

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

pandas/core/frame.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -2409,7 +2409,8 @@ def duplicated(self, cols=None, take_last=False):
24092409
#----------------------------------------------------------------------
24102410
# Sorting
24112411

2412-
def sort(self, columns=None, column=None, axis=0, ascending=True):
2412+
def sort(self, columns=None, column=None, axis=0, ascending=True,
2413+
inplace=False):
24132414
"""
24142415
Sort DataFrame either by labels (along either axis) or by the values in
24152416
column(s)
@@ -2423,6 +2424,8 @@ def sort(self, columns=None, column=None, axis=0, ascending=True):
24232424
Sort ascending vs. descending
24242425
axis : {0, 1}
24252426
Sort index/rows versus columns
2427+
inplace : boolean, default False
2428+
Sort the DataFrame without creating a new instance
24262429
24272430
Returns
24282431
-------
@@ -2432,9 +2435,10 @@ def sort(self, columns=None, column=None, axis=0, ascending=True):
24322435
import warnings
24332436
warnings.warn("column is deprecated, use columns", FutureWarning)
24342437
columns = column
2435-
return self.sort_index(by=columns, axis=axis, ascending=ascending)
2438+
return self.sort_index(by=columns, axis=axis, ascending=ascending,
2439+
inplace=inplace)
24362440

2437-
def sort_index(self, axis=0, by=None, ascending=True):
2441+
def sort_index(self, axis=0, by=None, ascending=True, inplace=False):
24382442
"""
24392443
Sort DataFrame either by labels (along either axis) or by the values in
24402444
a column
@@ -2448,6 +2452,8 @@ def sort_index(self, axis=0, by=None, ascending=True):
24482452
for a nested sort.
24492453
ascending : boolean, default True
24502454
Sort ascending vs. descending
2455+
inplace : boolean, default False
2456+
Sort the DataFrame without creating a new instance
24512457
24522458
Returns
24532459
-------
@@ -2470,7 +2476,18 @@ def sort_index(self, axis=0, by=None, ascending=True):
24702476
if not ascending:
24712477
indexer = indexer[::-1]
24722478

2473-
return self.take(indexer, axis=axis)
2479+
if inplace:
2480+
if axis == 1:
2481+
self._data = self._data.reindex_items(self._data.items[indexer],
2482+
copy=False)
2483+
elif axis == 0:
2484+
self._data = self._data.take(indexer)
2485+
else:
2486+
raise ValueError('Axis must be 0 or 1, got %s' % str(axis))
2487+
self._clear_item_cache()
2488+
return self
2489+
else:
2490+
return self.take(indexer, axis=axis)
24742491

24752492
def sortlevel(self, level=0, axis=0, ascending=True):
24762493
"""

pandas/core/internals.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,18 @@ def merge(self, other):
103103
return _merge_blocks([self, other], self.ref_items)
104104

105105
def reindex_axis(self, indexer, mask, needs_masking, axis=0,
106-
fill_value=np.nan, out=None):
106+
fill_value=np.nan):
107107
"""
108108
Reindex using pre-computed indexer information
109109
"""
110110
if self.values.size > 0:
111111
new_values = com.take_fast(self.values, indexer, mask,
112112
needs_masking, axis=axis,
113-
fill_value=fill_value,
114-
out=out)
113+
fill_value=fill_value)
115114
else:
116115
shape = list(self.shape)
117116
shape[axis] = len(indexer)
118-
if out is not None and (out.shape == shape):
119-
new_values = out
120-
else:
121-
new_values = np.empty(shape)
117+
new_values = np.empty(shape)
122118
new_values.fill(fill_value)
123119
return make_block(new_values, self.items, self.ref_items)
124120

pandas/tests/test_frame.py

+47
Original file line numberDiff line numberDiff line change
@@ -4557,6 +4557,53 @@ def test_sort_index_multicolumn(self):
45574557
expected = frame.take(indexer)
45584558
assert_frame_equal(result, expected)
45594559

4560+
def test_sort_index_inplace(self):
4561+
frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4],
4562+
columns=['A', 'B', 'C', 'D'])
4563+
4564+
# axis=0
4565+
unordered = frame.ix[[3, 2, 4, 1]]
4566+
df = unordered.copy()
4567+
df.sort_index(inplace=True)
4568+
expected = frame
4569+
assert_frame_equal(df, expected)
4570+
4571+
df = unordered.copy()
4572+
df.sort_index(ascending=False, inplace=True)
4573+
expected = frame[::-1]
4574+
assert_frame_equal(df, expected)
4575+
4576+
# axis=1
4577+
unordered = frame.ix[:, ['D', 'B', 'C', 'A']]
4578+
df = unordered.copy()
4579+
df.sort_index(axis=1, inplace=True)
4580+
expected = frame
4581+
assert_frame_equal(df, expected)
4582+
4583+
df = unordered.copy()
4584+
df.sort_index(axis=1, ascending=False, inplace=True)
4585+
expected = frame.ix[:, ::-1]
4586+
assert_frame_equal(df, expected)
4587+
4588+
def test_sort_inplace(self):
4589+
frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4],
4590+
columns=['A', 'B', 'C', 'D'])
4591+
4592+
sorted_df = frame.copy()
4593+
sorted_df.sort(columns='A', inplace=True)
4594+
expected = frame.sort_index(by='A')
4595+
assert_frame_equal(sorted_df, expected)
4596+
4597+
sorted_df = frame.copy()
4598+
sorted_df.sort(columns='A', ascending=False, inplace=True)
4599+
expected = frame.sort_index(by='A', ascending=False)
4600+
assert_frame_equal(sorted_df, expected)
4601+
4602+
sorted_df = frame.copy()
4603+
sorted_df.sort(columns=['A', 'B'], ascending=False, inplace=True)
4604+
expected = frame.sort_index(by=['A', 'B'], ascending=False)
4605+
assert_frame_equal(sorted_df, expected)
4606+
45604607
def test_frame_column_inplace_sort_exception(self):
45614608
s = self.frame['A']
45624609
self.assertRaises(Exception, s.sort)

0 commit comments

Comments
 (0)