0

I am running Python 3.6.5 and Pandas 0.25.2.

On attempting to style a pandas dataframe I am getting a specific error which can be generated by simplifying to this code:

import pandas as pd
import pandas.io.formats.style

The summary of the error generated is:

ImportError: The 'packaging._typing' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.

The full error message is:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-e9b944578fec> in <module>()
      1 import pandas as pd
----> 2 import pandas.io.formats.style

~\Anaconda3\lib\site-packages\pandas\io\formats\style.py in <module>()
     48 
     49 
---> 50 class Styler:
     51     """
     52     Helps style a DataFrame or Series according to the data with HTML and CSS.

~\Anaconda3\lib\site-packages\pandas\io\formats\style.py in Styler()
    109     """
    110 
--> 111     loader = jinja2.PackageLoader("pandas", "io/formats/templates")
    112     env = jinja2.Environment(loader=loader, trim_blocks=True)
    113     template = env.get_template("html.tpl")

~\Anaconda3\lib\site-packages\jinja2\loaders.py in __init__(self, package_name, package_path, encoding)
    220     def __init__(self, package_name, package_path='templates',
    221                  encoding='utf-8'):
--> 222         from pkg_resources import DefaultProvider, ResourceManager, \
    223                                   get_provider
    224         provider = get_provider(package_name)

~\Anaconda3\lib\site-packages\pkg_resources\__init__.py in <module>()
     79 from pkg_resources.extern import appdirs
     80 from pkg_resources.extern import packaging
---> 81 __import__('pkg_resources.extern.packaging.version')
     82 __import__('pkg_resources.extern.packaging.specifiers')
     83 __import__('pkg_resources.extern.packaging.requirements')

~\Anaconda3\lib\site-packages\pkg_resources\_vendor\packaging\version.py in <module>()
      9 
     10 from ._structures import Infinity, NegativeInfinity
---> 11 from ._typing import TYPE_CHECKING
     12 
     13 if TYPE_CHECKING:  # pragma: no cover

~\Anaconda3\lib\site-packages\pkg_resources\extern\__init__.py in load_module(self, fullname)
     52                 "normally this is bundled with this package so if you get "
     53                 "this warning, consult the packager of your "
---> 54                 "distribution.".format(**locals())
     55             )
     56 

I have tried reinstalling and upgrading the pandas installation, but each time I get the same error. This is being doine through an Anaconda environment.

Has anyone seen this error before? Is there a more detailed explanation that anyone can provide in an effort to solve this issue so that I can get the pandas styling working.

Thanks!

3 Answers 3

0

The correct way to do this is by:

from pandas.io.formats import style

This is because style is a module of pandas.io.formats package and the correct syntax is:

from package import module
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. In fact this is what I had originally. It generates exactly the same error though.
Well the thing I answered is the correct way to do it. And I verified it by running on Google Colab. So, it is evident now that the issue lies with the installation of your pandas then.
0

To clear up confusion, this doesn't work:

import pandas as pd
pd.io.formats.excel.ExcelFormatter.header_style = None 

But if you import the excel module from pandas.io.formats, then set the excel.ExcelFormatter.header_style property equal to None, it works! No more gross header formatting in your dataframe to Excel exports.

Do this to remove column headings/column names default formatting in Pandas.to_excel():

from pandas.io.formats import excel
excel.ExcelFormatter.header_style = None

This should produce Excel worksheets without the bold text and borders around the cells in the top row that pandas.to_excel() method adds by default.

If this doesn't work check your version of Pandas by typing this in a Jupyter Notebook or via the commandline interpreter:

import pandas as pd
print(pd.__version__)

My solution definitely works in the latest version of Pandas==2.0.3.

Comments

0

Install missing libraries manually

I had an issue with Dataframe.style too.

As seen in line 111 of the full error message, something is going on with jinja2 library. In my case that library was not installed. I installed it manually and the issue went away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.