Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sorting in dataframe editor #3240

Merged
merged 3 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions spyderlib/widgets/variableexplorer/dataframeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,20 @@ def sort(self, column, order=Qt.AscendingOrder):
"relation is defined for complex numbers")
return False
try:
ascending = order == Qt.AscendingOrder
if column > 0:
self.df.sort(columns=self.df.columns[column-1],
ascending=order, inplace=True)
try:
self.df.sort_values(by=self.df.columns[column-1],
ascending=ascending, inplace=True,
kind='mergesort')
except AttributeError:
# for pandas version < 0.17
self.df.sort(columns=self.df.columns[column-1],
ascending=ascending, inplace=True,
kind='mergesort')
self.update_df_index()
else:
self.df.sort_index(inplace=True, ascending=order)
self.df.sort_index(inplace=True, ascending=ascending)
self.update_df_index()
except TypeError as e:
QMessageBox.critical(self.dialog, "Error",
Expand Down
57 changes: 57 additions & 0 deletions spyderlib/widgets/variableexplorer/tests/test_dataframeeditor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2009- The Spyder Development Team
# Licensed under the terms of the MIT License

"""
Tests for dataframeeditor.py
"""

# Third party imports
from pandas import DataFrame
import pytest

# Local imports
from spyderlib.widgets.variableexplorer.dataframeeditor import DataFrameModel


# --- Tests
# -----------------------------------------------------------------------------
def test_dataframemodel_basic():
df = DataFrame({'colA': [1, 3], 'colB': ['c', 'a']})
dfm = DataFrameModel(df)
assert dfm.rowCount() == 2
assert dfm.columnCount() == 3
assert dfm.data(dfm.createIndex(0, 0)) == '0'
assert dfm.data(dfm.createIndex(0, 1)) == '1'
assert dfm.data(dfm.createIndex(0, 2)) == 'c'
assert dfm.data(dfm.createIndex(1, 0)) == '1'
assert dfm.data(dfm.createIndex(1, 1)) == '3'
assert dfm.data(dfm.createIndex(1, 2)) == 'a'

def test_dataframemodel_sort():
df = DataFrame({'colA': [1, 3], 'colB': ['c', 'a']})
dfm = DataFrameModel(df)
dfm.sort(2)
assert dfm.data(dfm.createIndex(0, 0)) == '1'
assert dfm.data(dfm.createIndex(0, 1)) == '3'
assert dfm.data(dfm.createIndex(0, 2)) == 'a'
assert dfm.data(dfm.createIndex(1, 0)) == '0'
assert dfm.data(dfm.createIndex(1, 1)) == '1'
assert dfm.data(dfm.createIndex(1, 2)) == 'c'

def test_dataframemodel_sort_is_stable(): # cf. issue 3010
df = DataFrame([[2,14], [2,13], [2,16], [1,3], [2,9], [1,15], [1,17],
[2,2], [2,10], [1,6], [2,5], [2,8], [1,11], [1,1],
[1,12], [1,4], [2,7]])
dfm = DataFrameModel(df)
dfm.sort(2)
dfm.sort(1)
col2 = [dfm.data(dfm.createIndex(i, 2)) for i in range(len(df))]
assert col2 == [str(x) for x in [1, 3, 4, 6, 11, 12, 15, 17,
2, 5, 7, 8, 9, 10, 13, 14, 16]]


if __name__ == "__main__":
pytest.main()