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!

agftrading
  • 784
  • 3
  • 8
  • 21

2 Answers2

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
Saurav Saha
  • 745
  • 1
  • 11
  • 30
  • Thanks for the reply. In fact this is what I had originally. It generates exactly the same error though. – agftrading Jan 25 '21 at 08:12
  • 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. – Saurav Saha Jan 26 '21 at 16:34
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.