Skip to content

Commit

Permalink
Gracefully handle ValueError in DataFrameModel
Browse files Browse the repository at this point in the history
This handles the case where the data frame contains a NaN and the user
specifies the format '%d'. Fixes #4139.
  • Loading branch information
jitseniesen committed Feb 13, 2017
1 parent 2cb8fb0 commit 983029c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions spyder/widgets/variableexplorer/dataframeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
# Used to convert bool intrance to false since bool('False') will return True
_bool_false = ['false', '0']

# Default format for data frames with floats
DEFAULT_FORMAT = '%.3g'

# Limit at which dataframe is considered so large that it is loaded on demand
LARGE_SIZE = 5e5
LARGE_NROWS = 1e5
Expand Down Expand Up @@ -83,7 +86,7 @@ class DataFrameModel(QAbstractTableModel):
ROWS_TO_LOAD = 500
COLS_TO_LOAD = 40

def __init__(self, dataFrame, format="%.3g", parent=None):
def __init__(self, dataFrame, format=DEFAULT_FORMAT, parent=None):
QAbstractTableModel.__init__(self)
self.dialog = parent
self.df = dataFrame
Expand Down Expand Up @@ -274,7 +277,12 @@ def data(self, index, role=Qt.DisplayRole):
else:
value = self.get_value(row, column-1)
if isinstance(value, float):
return to_qvariant(self._format % value)
try:
return to_qvariant(self._format % value)
except ValueError:
# may happen if format = '%d' and value = NaN;
# see issue 4139
return to_qvariant(DEFAULT_FORMAT % value)
else:
try:
return to_qvariant(to_text_string(value))
Expand Down

0 comments on commit 983029c

Please sign in to comment.