Skip to content

Commit

Permalink
Variable Explorer: Backport support for Pandas Series to the stable b…
Browse files Browse the repository at this point in the history
…ranch

Fixes #2815
  • Loading branch information
ccordoba12 committed Nov 18, 2015
1 parent 2ed3e1c commit 45379de
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 43 deletions.
4 changes: 2 additions & 2 deletions spyderlib/baseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ def get_supported_types():
except ImportError:
pass
try:
from pandas import DataFrame, TimeSeries
editable_types += [DataFrame, TimeSeries]
from pandas import DataFrame, Series
editable_types += [DataFrame, Series]
except ImportError:
pass
picklable_types = editable_types[:]
Expand Down
18 changes: 9 additions & 9 deletions spyderlib/widgets/dataframeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from spyderlib.utils import encoding
from spyderlib.widgets.arrayeditor import get_idx_rect

from pandas import DataFrame, TimeSeries
from pandas import DataFrame, Series
import numpy as np

# Supported Numbers and complex numbers
Expand Down Expand Up @@ -362,7 +362,7 @@ def fetch_more(self, rows=False, columns=False):

def columnCount(self, index=QModelIndex()):
"""DataFrame column number"""
# This is done to implement timeseries
# This is done to implement series
if len(self.df.shape) == 1:
return 2
elif self.total_cols <= self.cols_loaded:
Expand Down Expand Up @@ -472,7 +472,7 @@ def __init__(self, parent=None):
# (e.g. the editor's analysis thread in Spyder), thus leading to
# a segmentation fault on UNIX or an application crash on Windows
self.setAttribute(Qt.WA_DeleteOnClose)
self.is_time_series = False
self.is_series = False
self.layout = None

def setup_and_check(self, data, title=''):
Expand All @@ -487,8 +487,8 @@ def setup_and_check(self, data, title=''):
title = to_text_string(title) + " - %s" % data.__class__.__name__
else:
title = _("%s editor") % data.__class__.__name__
if isinstance(data, TimeSeries):
self.is_time_series = True
if isinstance(data, Series):
self.is_series = True
data = data.to_frame()

self.setWindowTitle(title)
Expand Down Expand Up @@ -521,7 +521,7 @@ def setup_and_check(self, data, title=''):

self.bgcolor_global = QCheckBox(_('Column min/max'))
self.bgcolor_global.setChecked(self.dataModel.colum_avg_enabled)
self.bgcolor_global.setEnabled(not self.is_time_series and
self.bgcolor_global.setEnabled(not self.is_series and
self.dataModel.bgcolor_enabled)
self.connect(self.bgcolor_global, SIGNAL("stateChanged(int)"),
self.dataModel.colum_avg)
Expand All @@ -542,7 +542,7 @@ def change_bgcolor_enable(self, state):
This is implementet so column min/max is only active when bgcolor is
"""
self.dataModel.bgcolor(state)
self.bgcolor_global.setEnabled(not self.is_time_series and state > 0)
self.bgcolor_global.setEnabled(not self.is_series and state > 0)

def change_format(self):
"""Change display format"""
Expand All @@ -565,7 +565,7 @@ def get_value(self):
# It is import to avoid accessing Qt C++ object as it has probably
# already been destroyed, due to the Qt.WA_DeleteOnClose attribute
df = self.dataModel.get_data()
if self.is_time_series:
if self.is_series:
return df.iloc[:, 0]
else:
return df
Expand Down Expand Up @@ -614,7 +614,7 @@ def test():
df1.sort(columns=[0, 1], inplace=True)
out = test_edit(df1)
print("out:", out)
out = test_edit(TimeSeries(np.arange(10)))
out = test_edit(Series(np.arange(10)))
print("out:", out)
return out

Expand Down
26 changes: 13 additions & 13 deletions spyderlib/widgets/dicteditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
get_human_readable_type, value_to_display, get_color_name,
is_known_type, FakeObject, Image, ndarray, array, MaskedArray,
unsorted_unique, try_to_eval, datestr_to_datetime,
get_numpy_dtype, is_editable_type, DataFrame, TimeSeries)
get_numpy_dtype, is_editable_type, DataFrame, Series)
if ndarray is not FakeObject:
from spyderlib.widgets.arrayeditor import ArrayEditor
if DataFrame is not FakeObject:
Expand Down Expand Up @@ -496,9 +496,9 @@ def createEditor(self, parent, option, index):
key=key, readonly=readonly,
conv=conv_func))
return None
#--editor = DataFrameEditor and TimeSeriesEditor
elif isinstance(value, (DataFrame, TimeSeries))\
and DataFrame is not FakeObject:
#--editor = DataFrameEditor
elif isinstance(value, (DataFrame, Series)) \
and DataFrame is not FakeObject:
editor = DataFrameEditor()
if not editor.setup_and_check(value, title=key):
return
Expand Down Expand Up @@ -1316,18 +1316,18 @@ def __init__(self, parent, data, truncate=True, minmax=False,
is_array_func=None, is_image_func=None, is_dict_func=None,
get_array_shape_func=None, get_array_ndim_func=None,
oedit_func=None, plot_func=None, imshow_func=None,
is_data_frame_func=None, is_time_series_func=None,
is_data_frame_func=None, is_series_func=None,
show_image_func=None, remote_editing=False):
BaseTableView.__init__(self, parent)

self.remote_editing_enabled = None

self.remove_values = remove_values_func
self.copy_value = copy_value_func
self.new_value = new_value_func

self.is_data_frame = is_data_frame_func
self.is_time_series = is_time_series_func
self.is_series = is_series_func
self.is_list = is_list_func
self.get_len = get_len_func
self.is_array = is_array_func
Expand Down Expand Up @@ -1358,15 +1358,15 @@ def setup_menu(self, truncate, minmax):
"""Setup context menu"""
menu = BaseTableView.setup_menu(self, truncate, minmax)
return menu

def oedit_possible(self, key):
if (self.is_list(key) or self.is_dict(key)
if (self.is_list(key) or self.is_dict(key)
or self.is_array(key) or self.is_image(key)
or self.is_data_frame(key) or self.is_time_series(key)):
# If this is a remote dict editor, the following avoid
or self.is_data_frame(key) or self.is_series(key)):
# If this is a remote dict editor, the following avoid
# transfering large amount of data through the socket
return True

def edit_item(self):
"""
Reimplement BaseTableView's method to edit item
Expand Down
12 changes: 6 additions & 6 deletions spyderlib/widgets/dicteditorutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def get_numpy_dtype(obj):
"Variable Explorer"),
required_version=PANDAS_REQVER)
if programs.is_module_installed('pandas', PANDAS_REQVER):
from pandas import DataFrame, TimeSeries
from pandas import DataFrame, Series
else:
DataFrame = TimeSeries = FakeObject # analysis:ignore
DataFrame = Series = FakeObject # analysis:ignore


#----PIL Images support
Expand Down Expand Up @@ -126,7 +126,7 @@ def datestr_to_datetime(value):
MaskedArray,
matrix,
DataFrame,
TimeSeries): ARRAY_COLOR,
Series): ARRAY_COLOR,
Image: "#008000",
datetime.date: "#808000",
}
Expand Down Expand Up @@ -234,7 +234,7 @@ def get_size(item):
return item.shape
elif isinstance(item, Image):
return item.size
if isinstance(item, (DataFrame, TimeSeries)):
if isinstance(item, (DataFrame, Series)):
return item.shape
else:
return 1
Expand All @@ -243,8 +243,8 @@ def get_type_string(item):
"""Return type string of an object"""
if isinstance(item, DataFrame):
return "DataFrame"
if isinstance(item, TimeSeries):
return "TimeSeries"
if isinstance(item, Series):
return "Series"
found = re.findall(r"<(?:type|class) '(\S*)'>", str(type(item)))
if found:
return found[0]
Expand Down
16 changes: 8 additions & 8 deletions spyderlib/widgets/externalshell/namespacebrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def setup(self, check_all=None, exclude_private=None,
is_image_func=self.is_image,
is_dict_func=self.is_dict,
is_data_frame_func=self.is_data_frame,
is_time_series_func=self.is_time_series,
is_series_func=self.is_series,
get_array_shape_func=self.get_array_shape,
get_array_ndim_func=self.get_array_ndim,
oedit_func=self.oedit,
Expand Down Expand Up @@ -377,17 +377,17 @@ def is_array(self, name):
def is_image(self, name):
"""Return True if variable is a PIL.Image image"""
return communicate(self._get_sock(), 'is_image("%s")' % name)

def is_data_frame(self, name):
"""Return True if variable is a data_frame"""
"""Return True if variable is a DataFrame"""
return communicate(self._get_sock(),
"isinstance(globals()['%s'], DataFrame)" % name)
def is_time_series(self, name):
"""Return True if variable is a data_frame"""

def is_series(self, name):
"""Return True if variable is a Series"""
return communicate(self._get_sock(),
"isinstance(globals()['%s'], TimeSeries)" % name)
"isinstance(globals()['%s'], Series)" % name)

def get_array_shape(self, name):
"""Return array's shape"""
return communicate(self._get_sock(), "%s.shape" % name)
Expand Down
9 changes: 4 additions & 5 deletions spyderlib/widgets/objecteditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create_dialog(obj, obj_name):
from spyderlib.widgets.texteditor import TextEditor
from spyderlib.widgets.dicteditorutils import (ndarray, FakeObject,
Image, is_known_type,
DataFrame, TimeSeries)
DataFrame, Series)
from spyderlib.widgets.dicteditor import DictEditor
from spyderlib.widgets.arrayeditor import ArrayEditor
if DataFrame is not FakeObject:
Expand All @@ -73,7 +73,7 @@ def create_dialog(obj, obj_name):
readonly=readonly):
return
elif isinstance(obj, Image) and Image is not FakeObject \
and ndarray is not FakeObject:
and ndarray is not FakeObject:
dialog = ArrayEditor()
import numpy as np
data = np.array(obj)
Expand All @@ -82,11 +82,10 @@ def create_dialog(obj, obj_name):
return
from spyderlib.pil_patch import Image
conv_func = lambda data: Image.fromarray(data, mode=obj.mode)
elif isinstance(obj, (DataFrame, TimeSeries)) \
and DataFrame is not FakeObject:
elif isinstance(obj, (DataFrame, Series)) and DataFrame is not FakeObject:
dialog = DataFrameEditor()
if not dialog.setup_and_check(obj):
return
return
elif is_text_string(obj):
dialog = TextEditor(obj, title=obj_name, readonly=readonly)
else:
Expand Down

0 comments on commit 45379de

Please sign in to comment.