-
-
Notifications
You must be signed in to change notification settings - Fork 17.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Wrong Custom Formatters applied when displaying trancated frames #35410
Comments
Thanks for reporting this - could you provide a minimal reproducible example of the data/code that demonstrates the issue? |
import pandas as pd
def form(name):
return lambda x: f"{name}: {x}"
df = pd.DataFrame({f"Col{x}":range(5) for x in range(6)})
print(df.to_string(formatters=formatters, max_cols=6))
print(df.to_string(formatters={c: form(c) for c in df}, max_cols=4)) produces:
As you can see the second print uses wrong formatters after the truncated columns I have patched my version as suggested above: def _format_col(self, i: int) -> List[str]:
frame = self.tr_frame
formatter = self._get_formatter(frame.columns[i]) # instead of _get_formatter(i) |
Thanks - I can reproduce on master once I replace I think the root cause of the issue is that the Would you be interested in submitting a PR to fix? |
I have never contributed to pandas development, and don't know the procedure. In addition to that, I have tried to follow your lead to see if |
Problem description
I am providing custom formatters for specific columns as dict.
If frame is large enough and some columns are truncated - then wrong formatters are applied to the columns.
(In my case that leads to crushes as wrong data type is received by the formatter).
Please notice, that behavior changes depending on the width of the console window as different columns are displayed.
Problem investigation
I have examined the code of my version of panda (1.0.5) and compared with the last version in GitHub - the bug seems to be still there.
The source of the problem starts with this method (
DataFrameFormatter._to_str_columns
), whenframe is set to truncated
frame = self.tr_frame
and thenself._format_col(i)
is called with index of the column in the TRUNCATED frame:Then this "truncated" column index is passed to
self._get_formatter
:which uses full frame columns to retrieve formatter using index
i
which corresponds to the columns of the truncated frame:INSTALLED VERSIONS
commit : None
python : 3.6.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-37-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.5
numpy : 1.18.5
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.0.0.post20200309
Cython : 0.29.15
pytest : 5.4.1
hypothesis : 5.19.3
sphinx : 2.4.0
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.16.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : 3.4.4
tabulate : 0.8.3
xarray : 0.15.0
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.8
numba : 0.50.1
The text was updated successfully, but these errors were encountered: