'How can I use pandas Styler.to_latex() if I have a Series instead of a Dataframe?
I've been using the Series.to_latex() method to generate a table in my LaTeX doc as it follows:
section.append(NoEscape(s.transform(lambda x: f"{x:,}").to_latex()))
My Serie looks like this:
All channels 1099592
Telefe 158738
El Trece 127082
America TV 109763
TN 64598
El Nueve 43450
A24 37639
C5N 36800
ESPN 29356
LN + 27006
Cronica TV 25140
Name: Total:, dtype: int64
But since yesterday my script shows the following output:
FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.
section.append(NoEscape(s.transform(lambda x: f"{x:,}").to_latex()))
But when I try to implement the s.style.to_latex() or s.style.format(thousands = ",").to_latex() statements I get the following error:
AttributeError: 'Series' object has no attribute 'style'
I'm not sure the style is properly implemented for Series since i haven't found any indication of in in the API reference
My question here is if there's a way to prevent this warning from happening since it might be depricated in the future, and to optimize my code
Thanks in advance
Solution 1:[1]
The error does say the attribute is not for a series, while the warning shows to be applied to a dataframe. I would try:
pd.DataFrame(s).style.to_latex()
or
pd.DataFrame(s).style.format(thousands = ",").to_latex()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | chitown88 |
