You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When trying to print a dataframe, if the values contained in the indexes and/or in the first/last column are too long and the terminal too narrow, an IndexError is raised:
This happens because pandas.io.formats.format.DataFrameFormatter._chk_truncate() gives an empty frame. Subsequently, an empty str_columns is passed to pandas.core.indexes.multi._sparsify(*args) which raises the IndexError when trying to access the passed variable.
This problem seems to arise only when the dataframe and its index contains datetime-like values (see example with long strings in next section).
Expected Output
The raised exception is misleading, since the user could think his/her dataframe has something wrong, whereas only the size of the terminal is too narrow. A proper slicing of the string version of datetime formats in order to fit the terminal size would be the expected output.
With strings, this works better than with datetime. The following code does not raise any Exception:
n=10rng=pd.date_range('22/12/2018', periods=n, freq='H')
id_1= ["i am a really long string"]*n
["Lorem ipsum dolor sit amet, consectetur adipiscing elit"+str(i) foriinrange(n)]
ids=pd.MultiIndex.from_tuples(list(zip(id_1,id_2,rng)))
df=pd.DataFrame([rng,rng]).transpose()
df.index=idsprint(df)
Otherwise, a warning with an explanation should be raised if the frame that has to be printed gets truncated to an empty one.
Code Sample, a copy-pastable example if possible
Execute this code in a terminal with width <= 90 cols.
Problem description
When trying to print a dataframe, if the values contained in the indexes and/or in the first/last column are too long and the terminal too narrow, an IndexError is raised:
This happens because
pandas.io.formats.format.DataFrameFormatter._chk_truncate()
gives an empty frame. Subsequently, an emptystr_columns
is passed topandas.core.indexes.multi._sparsify(*args)
which raises theIndexError
when trying to access the passed variable.This problem seems to arise only when the dataframe and its index contains
datetime
-like values (see example with long strings in next section).Expected Output
The raised exception is misleading, since the user could think his/her dataframe has something wrong, whereas only the size of the terminal is too narrow. A proper slicing of the string version of datetime formats in order to fit the terminal size would be the expected output.
With strings, this works better than with datetime. The following code does not raise any Exception:
Otherwise, a warning with an explanation should be raised if the frame that has to be printed gets truncated to an empty one.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-22-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.23.0
pytest: None
pip: 9.0.1
setuptools: 39.2.0
Cython: 0.28.3
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.6.7
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: