fixes UnboundLocalError when displaying empty dataframes

This commit is contained in:
Michael Hirsch 2019-11-19 15:52:03 -05:00
parent c90602dd65
commit f1ec6c0d8b
2 changed files with 47 additions and 6 deletions

View File

@ -605,7 +605,7 @@ class DataFrame(NDFrame):
if show_dimensions:
# TODO - this results in different output to pandas
# TODO - the 'x' character is different and this gets added after the </div>
_buf.write("\n<p>{nrows} rows x {ncols} columns</p>"
_buf.write("\n<p>{nrows} rows × {ncols} columns</p>"
.format(nrows=len(self.index), ncols=len(self.columns)))
if buf is None:
@ -671,10 +671,6 @@ class DataFrame(NDFrame):
decimal=decimal,
line_width=line_width)
# dimensions are not show in the terminal, but they are shown in jupyter
if len(self) == 0:
show_dimensions=False
# Our fake dataframe has incorrect number of rows (max_rows*2+1) - write out
# the correct number of rows
if show_dimensions:

View File

@ -52,6 +52,15 @@ class TestDataFrameRepr(TestData):
assert pd_head_str == ed_head_str
def test_empty_dataframe_string(self):
ed_ecom = self.ed_ecommerce()
pd_ecom = self.pd_ecommerce()
ed_ecom_s = ed_ecom[ed_ecom['currency'] == 'USD'].to_string()
pd_ecom_s = pd_ecom[pd_ecom['currency'] == 'USD'].to_string()
assert ed_ecom_s == pd_ecom_s
"""
repr
"""
@ -82,6 +91,15 @@ class TestDataFrameRepr(TestData):
assert pd_head_str == ed_head_str
def test_empty_dataframe_repr(self):
ed_ecom = self.ed_ecommerce()
pd_ecom = self.pd_ecommerce()
ed_ecom_r = repr(ed_ecom[ed_ecom['currency'] == 'USD'])
pd_ecom_r = repr(pd_ecom[pd_ecom['currency'] == 'USD'])
assert ed_ecom_r == pd_ecom_r
"""
to_html
"""
@ -118,6 +136,15 @@ class TestDataFrameRepr(TestData):
assert pd_head_str == ed_head_str
def test_empty_dataframe_to_html(self):
ed_ecom = self.ed_ecommerce()
pd_ecom = self.pd_ecommerce()
ed_ecom_h = ed_ecom[ed_ecom['currency'] == 'USD'].to_html()
pd_ecom_h = pd_ecom[pd_ecom['currency'] == 'USD'].to_html()
assert ed_ecom_h == pd_ecom_h
"""
_repr_html_
@ -157,3 +184,21 @@ class TestDataFrameRepr(TestData):
#print(pd_head_str)
assert pd_head_str == ed_head_str
def test_empty_dataframe_repr_html(self):
# TODO - there is a bug in 'show_dimensions' as it gets added after the last </div>
# For now test without this
show_dimensions = pd.get_option('display.show_dimensions')
pd.set_option('display.show_dimensions', False)
ed_ecom = self.ed_ecommerce()
pd_ecom = self.pd_ecommerce()
ed_ecom_rh = ed_ecom[ed_ecom['currency'] == 'USD']._repr_html_()
pd_ecom_rh = pd_ecom[pd_ecom['currency'] == 'USD']._repr_html_()
# Restore default
pd.set_option('display.show_dimensions', show_dimensions)
assert ed_ecom_rh == pd_ecom_rh