Skip to content

BUG: UnboundLocalError on df.to_string(max_rows=0) #35394

@jjhale

Description

@jjhale
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
df.to_string(max_rows=0)

example running it:

>>> import pandas as pd
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
>>> df = pd.DataFrame(data=d)
>>> df.to_string()
'   col1  col2\n0     1     3\n1     2     4'
>>> df.to_string(max_rows=1)
'    col1  col2\n0      1     3\n..   ...   ...'
>>> df.to_string(max_rows=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 818, in to_string
    line_width=line_width,
  File "/usr/local/lib/python3.7/site-packages/pandas/io/formats/format.py", line 612, in __init__
    self._chk_truncate()
  File "/usr/local/lib/python3.7/site-packages/pandas/io/formats/format.py", line 640, in _chk_truncate
    n_add_rows = self.header + dot_row + show_dimension_rows + prompt_row
UnboundLocalError: local variable 'show_dimension_rows' referenced before assignment

Problem description

Throwing an unchecked exception on a valid input.
The variable show_dimension_rows is only conditionally defined here:

https://github.com/pandas-dev/pandas/blob/v1.0.5/pandas/io/formats/format.py#L636

A simple fix could be to include an else block and set it to zero.

Expected Output

Ideally it would output no data rows (if the df did have some rows):

>>> df.to_string(max_rows=0)
'    col1  col2\n..   ...   ...'

or the standard empty data frame message if it had zero rows:

>>> d = {'col1': [], 'col2': []}
>>> df = pd.DataFrame(data=d)
>>> df.to_string(max_rows=0)
'Empty DataFrame\nColumns: [col1, col2]\nIndex: []'

In the simple fix case, it would assume that it was in the terminal and output as if max_rows had not been set, which would probably be better than the exception.

>>> d = {'col1': [1, 2], 'col2': [3, 4]}
>>> df = pd.DataFrame(data=d)
>>> df.to_string(max_rows=0)
'   col1  col2\n0     1     3\n1     2     4'
>>> df.to_string()
'   col1  col2\n0     1     3\n1     2     4'

Output of pd.show_versions()

Details
>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.6.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 19.6.0
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.0.5
numpy            : 1.19.1
pytz             : 2020.1
dateutil         : 2.8.0
pip              : 19.3.1
setuptools       : 42.0.2
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
pytest           : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
numba            : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions