Skip to content

Commit c48d89a

Browse files
committed
Added rename and applyFunction methods
1 parent 3267dab commit c48d89a

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

qtpandas/models/DataFrameModel.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,48 @@ def timestampFormat(self, timestampFormat):
196196
#assert isinstance(timestampFormat, unicode) or timestampFormat.__class__.__name__ == "DateFormat", "not of type unicode"
197197
self._timestampFormat = timestampFormat
198198

199+
def rename(self, index=None, columns=None, **kwargs):
200+
"""
201+
Renames the dataframe inplace calling appropriate signals.
202+
Wraps pandas.DataFrame.rename(*args, **kwargs) - overrides
203+
the inplace kwarg setting it to True.
204+
205+
Example use:
206+
renames = {'colname1':'COLNAME_1', 'colname2':'COL2'}
207+
DataFrameModel.rename(columns=renames)
208+
209+
:param args:
210+
see pandas.DataFrame.rename
211+
:param kwargs:
212+
see pandas.DataFrame.rename
213+
:return:
214+
True on success, False on failure.
215+
"""
216+
kwargs['inplace'] = True
217+
self.layoutAboutToBeChanged.emit()
218+
self._dataFrame.rename(index, columns, **kwargs)
219+
self.layoutChanged.emit()
220+
self.dataChanged.emit()
221+
self.dataFrameChanged.emit()
222+
223+
def applyFunction(self, func):
224+
"""
225+
Applies a function to the dataFrame with appropriate signals.
226+
:param func: A function (or partial function) that accepts a dataframe as the first argument.
227+
:return: None
228+
:raise:
229+
AssertionError if the func is not callable.
230+
AssertionError if the func does not return a DataFrame.
231+
"""
232+
assert callable(func), "function {} is not callable".format(func)
233+
self.layoutAboutToBeChanged.emit()
234+
df = func(self._dataFrame)
235+
assert isinstance(df, pandas.DataFrame), "method {} did not return a DataFrame.".format(func.__name__)
236+
self._dataFrame = df
237+
self.layoutChanged.emit()
238+
self.dataChanged.emit()
239+
self.dataFrameChanged.emit()
240+
199241
def headerData(self, section, orientation, role=Qt.DisplayRole):
200242
"""
201243
Return the header depending on section, orientation and Qt::ItemDataRole

tests/test_DataFrameModel.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from qtpandas.compat import Qt, QtCore, QtGui
55

6-
76
import pytest
87
import pytestqt
98

@@ -590,6 +589,34 @@ def newColumns(self):
590589

591590
return columns
592591

592+
def test_rename(self, model, dataFrame):
593+
renames = {'Foo':'Booyah', 'Bar':'Boogam'}
594+
cols = dataFrame.columns.tolist()
595+
assert not 'Booyah' in cols and not 'Boogam' in cols
596+
model.rename(columns=renames)
597+
cols = model._dataFrame.columns.tolist()
598+
assert 'Booyah' in cols and 'Boogam' in cols
599+
assert 'Foo' not in cols and 'Bar' not in cols
600+
601+
def test_apply_function(self, model):
602+
def mini_func(df):
603+
return df
604+
def bad_func(df):
605+
return False
606+
607+
model.applyFunction(mini_func)
608+
609+
expected = False
610+
try:
611+
model.applyFunction(bad_func)
612+
except:
613+
expected = True
614+
pass
615+
616+
assert expected
617+
618+
619+
593620
def test_edit_data(self, model):
594621
index = model.index(0, 0)
595622
currentData = index.data()

0 commit comments

Comments
 (0)