From cb532cd236e344ee0d0c5b894c064356d8ec4775 Mon Sep 17 00:00:00 2001 From: Elizaveta Shashkova Date: Tue, 9 Jun 2020 20:18:29 +0300 Subject: [PATCH] PY-34650 Python console crashes when trying to view a DataFrame with a Int64 column with nans Use default formatter if formatting failed Signed-off-by: Elizaveta Shashkova GitOrigin-RevId: 90062c52ffe55c9fa74e41b3a4bbe6d4c6211ce1 --- .../pydev/_pydevd_bundle/pydevd_thrift.py | 9 +++++---- .../helpers/pydev/_pydevd_bundle/pydevd_vars.py | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/python/helpers/pydev/_pydevd_bundle/pydevd_thrift.py b/python/helpers/pydev/_pydevd_bundle/pydevd_thrift.py index 8a1c6e39475c..157ca082b739 100644 --- a/python/helpers/pydev/_pydevd_bundle/pydevd_thrift.py +++ b/python/helpers/pydev/_pydevd_bundle/pydevd_thrift.py @@ -16,7 +16,7 @@ from _pydevd_bundle.pydevd_extension_api import TypeResolveProvider, StrPresenta from _pydevd_bundle.pydevd_utils import take_first_n_coll_elements, is_numeric_container, is_pandas_container, is_string, pandas_to_str, \ should_evaluate_full_value, should_evaluate_shape from _pydevd_bundle.pydevd_vars import get_label, array_default_format, is_able_to_format_number, MAXIMUM_ARRAY_SIZE, \ - get_column_formatter_by_type, DEFAULT_DF_FORMAT + get_column_formatter_by_type, get_formatted_row_elements, DEFAULT_DF_FORMAT from pydev_console.pydev_protocol import DebugValue, GetArrayResponse, ArrayData, ArrayHeaders, ColHeader, RowHeader, \ UnsupportedArrayTypeException, ExceedingArrayDimensionsException @@ -549,10 +549,11 @@ def dataframe_to_thrift_struct(df, name, roffset, coffset, rows, cols, format): iat = df.iat if dim == 1 or len(df.columns.unique()) == len(df.columns) else df.iloc + def formatted_row_elements(row): + return get_formatted_row_elements(row, iat, dim, cols, format, dtypes) + array_chunk.headers = header_data_to_thrift_struct(rows, cols, dtypes, col_bounds, col_to_format, df, dim) - array_chunk.data = array_data_to_thrift_struct(rows, cols, - lambda r: (("%" + col_to_format(c)) % (iat[r, c] if dim > 1 else iat[r]) - for c in range(cols)), format) + array_chunk.data = array_data_to_thrift_struct(rows, cols, formatted_row_elements, format) return array_chunk diff --git a/python/helpers/pydev/_pydevd_bundle/pydevd_vars.py b/python/helpers/pydev/_pydevd_bundle/pydevd_vars.py index b43afd073b6e..5d692ba1ddf3 100644 --- a/python/helpers/pydev/_pydevd_bundle/pydevd_vars.py +++ b/python/helpers/pydev/_pydevd_bundle/pydevd_vars.py @@ -595,6 +595,16 @@ def get_column_formatter_by_type(initial_format, column_type): return array_default_format(column_type) +def get_formatted_row_elements(row, iat, dim, cols, format, dtypes): + for c in range(cols): + val = iat[row, c] if dim > 1 else iat[row] + col_formatter = get_column_formatter_by_type(format, dtypes[c]) + try: + yield ("%" + col_formatter) % val + except TypeError: + yield ("%" + DEFAULT_DF_FORMAT) % val + + def array_default_format(type): if type == 'f': return '.5f' @@ -672,9 +682,11 @@ def dataframe_to_xml(df, name, roffset, coffset, rows, cols, format): iat = df.iat if dim == 1 or len(df.columns.unique()) == len(df.columns) else df.iloc + def formatted_row_elements(row): + return get_formatted_row_elements(row, iat, dim, cols, format, dtypes) + xml += header_data_to_xml(rows, cols, dtypes, col_bounds, col_to_format, df, dim) - xml += array_data_to_xml(rows, cols, lambda r: (("%" + col_to_format(c)) % (iat[r, c] if dim > 1 else iat[r]) - for c in range(cols)), format) + xml += array_data_to_xml(rows, cols, formatted_row_elements, format) return xml