Skip to content
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: decimal=',' does not work properly with complex values #41636

Closed
1 task
pjrandewijk opened this issue May 23, 2021 · 2 comments · Fixed by #40422
Closed
1 task

BUG: decimal=',' does not work properly with complex values #41636

pjrandewijk opened this issue May 23, 2021 · 2 comments · Fixed by #40422
Labels
Milestone

Comments

@pjrandewijk
Copy link

  • [ x] I have checked that this issue has not already been reported.

  • [ x] 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

# Your code here
import pandas as pd
Y0=array([[0.-20.j        , 0. +0.j ],
                [0. +0.j        , 0.-12.j  ]])
Y0df=pd.DataFrame(Y0)
print(Y0df.to_latex(header=False,index=False,decimal=','), file=open('Y0.tex', 'w'))

Problem description

The contence of Y0.tex is shown below with only the real part having the correct decimal symbol.

\begin{tabular}{rr}
\toprule
0,000000-20.000000j & 0,000000+0.000000j \
0,000000+0.000000j & 0,000000-12.000000j \
\bottomrule
\end{tabular}

Expected Output

\begin{tabular}{rr}
\toprule
0,000000-20,000000j & 0,000000+0,000000j \
0,000000+0.000000j & 0,000000-12,000000j \
\bottomrule
\end{tabular}

Desired Output

#instead of using 'decimal' an 'use_numprint=True' would be nicer to allow for custom customisation within LaTeX, which would ideally produce the following output
%uncomment the following line if not already defined
%\newcommand{\np}[2][]{\numprint[#1]{#2}}
\begin{tabular}{rr}
\toprule
\np{0.000000}-\np{20.000000}j & \np{0.000000}+\np{0.000000}j \
\np{0.000000}+\np{0.000000}j & \np{0.000000}-\np{12.000000}j \
\bottomrule
\end{tabular}

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : English_South Africa.1252

pandas : 1.1.3
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 50.3.1.post20201107
Cython : 0.29.21
pytest : 6.1.1
hypothesis : None
sphinx : 3.2.1
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.3
fastparquet : None
gcsfs : None
matplotlib : 3.3.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : 1.3.20
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.51.2

@pjrandewijk pjrandewijk added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 23, 2021
@rhshadrach
Copy link
Member

Thanks for the report! Could you open a new issue for the enhancement request?

Further investigations and PRs to fix are welcome!

@rhshadrach rhshadrach added IO LaTeX to_latex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 24, 2021
@rhshadrach rhshadrach added this to the Contributions Welcome milestone May 24, 2021
@attack68
Copy link
Contributor

attack68 commented May 24, 2021

If/When Styler.to_latex deprecates DataFrame.to_latex this fix is not required.
This will become available already after Styler.to_latex: #40422.
That PR specifically excludes formatting from the to_latex call for reasons stated in PR. The .format call is required.

Y0=np.array([[0.-20.j        , 0. +1000.j ],
             [0. +0.j        , 1000.-12.j  ]])
Y0df=pd.DataFrame(Y0)
print(Y0df.style.format(decimal=",", precision=2, thousands=".").to_latex())

\begin{tabular}{lrr}
{} & {0} & {1} \\
0 & 0,00-20,00j & 0,00+1.000,00j \\
1 & 0,00+0,00j & 1.000,00-12,00j \\
\end{tabular}

EDIT:
And here would be the solution to your requested format using a custom formatter:

def f(x):
    r = f"{x.real:,.3f}".replace(",", "#").replace(".", ",").replace("#", ".")
    i = f"{x.imag:,.3f}".replace(",", "#").replace(".", ",").replace("#", ".")
    return f"\\np{{{r}}}{'+' if x.imag >= 0 else '-'}\\np{{{i}}}j"

Y0=np.array([[0.-20.j        , 0. +1000.j ],
             [0. +0.j        , 1000.-12.j  ]])
Y0df=pd.DataFrame(Y0)
print(Y0df.style.format(f).to_latex())

\begin{tabular}{lrr}
{} & {0} & {1} \\
0 & \np{0,000}-\np{-20,000}j & \np{0,000}+\np{1.000,000}j \\
1 & \np{0,000}+\np{0,000}j & \np{1.000,000}-\np{-12,000}j \\
\end{tabular}

@jreback jreback modified the milestones: Contributions Welcome, 1.3 May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants