Skip to content

Cannot style.background_gradient on an Int64 column #28869

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

Closed
eddy-geek opened this issue Oct 9, 2019 · 5 comments
Closed

Cannot style.background_gradient on an Int64 column #28869

eddy-geek opened this issue Oct 9, 2019 · 5 comments
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. good first issue Needs Tests Unit test(s) needed to prevent regressions Styler conditional formatting using DataFrame.style

Comments

@eddy-geek
Copy link

old = pd.Series(range(9))
new = pd.Series(range(9), dtype='Int64')

print(old.min(), old.values.min(), new.min())  # =0 OK
print(new.values.min()) # KO

old.to_frame().style.background_gradient() # OK
new.to_frame().style.background_gradient() # KO

Problem description

Similarly to #25580, the newer Int64 backend is missing some basic functionality.

Expected: values.min() is available, gradients are displayed

Actual:

----> 1 print(new.values.min())
AttributeError: 'IntegerArray' object has no attribute 'min'

and full stack of the background_gradient error:

AttributeError                            Traceback (most recent call last)
~/virtualenvs/jupyter/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/io/formats/style.py in _repr_html_(self)
    161         Hooks into Jupyter notebook rich display system.
    162         """
--> 163         return self.render()
    164 
    165     @Appender(_shared_docs['to_excel'] % dict(

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/io/formats/style.py in render(self, **kwargs)
    457         * table_attributes
    458         """
--> 459         self._compute()
    460         # TODO: namespace all the pandas keys
    461         d = self._translate()

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/io/formats/style.py in _compute(self)
    527         r = self
    528         for func, args, kwargs in self._todo:
--> 529             r = func(self)(*args, **kwargs)
    530         return r
    531 

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/io/formats/style.py in _apply(self, func, axis, subset, **kwargs)
    536         if axis is not None:
    537             result = data.apply(func, axis=axis,
--> 538                                 result_type='expand', **kwargs)
    539             result.columns = data.columns
    540         else:

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6485                          args=args,
   6486                          kwds=kwds)
-> 6487         return op.get_result()
   6488 
   6489     def applymap(self, func):

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/core/apply.py in get_result(self)
    149             return self.apply_raw()
    150 
--> 151         return self.apply_standard()
    152 
    153     def apply_empty_result(self):

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/core/apply.py in apply_standard(self)
    255 
    256         # compute the result using the series generator
--> 257         self.apply_series_generator()
    258 
    259         # wrap results

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/core/apply.py in apply_series_generator(self)
    284             try:
    285                 for i, v in enumerate(series_gen):
--> 286                     results[i] = self.f(v)
    287                     keys.append(v.name)
    288             except Exception as e:

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/core/apply.py in f(x)
     76 
     77             def f(x):
---> 78                 return func(x, *args, **kwds)
     79         else:
     80             f = func

~/virtualenvs/jupyter/lib/python3.6/site-packages/pandas/io/formats/style.py in _background_gradient(s, cmap, low, high, text_color_threshold)
    939 
    940         with _mpl(Styler.background_gradient) as (plt, colors):
--> 941             smin = s.values.min()
    942             smax = s.values.max()
    943             rng = smax - smin

AttributeError: ("'IntegerArray' object has no attribute 'min'", 'occurred at index 0')

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-1062.1.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 5.0.1
pip: 19.2.3
setuptools: 41.0.1
Cython: None
numpy: 1.16.3
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.1.0
openpyxl: None
xlrd: 1.2.0
xlwt: None
xlsxwriter: None
lxml.etree: 4.3.3
bs4: None
html5lib: None
sqlalchemy: 1.3.3
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

immaxchen added a commit to immaxchen/pandas that referenced this issue Oct 27, 2019
Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
TomAugspurger pushed a commit that referenced this issue Nov 3, 2019
…29245)

* ENH: Styler.background_gradient to accept vmin vmax and dtype Int64

Resolve #12145 and #28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
Reksbril pushed a commit to Reksbril/pandas that referenced this issue Nov 18, 2019
…andas-dev#29245)

* ENH: Styler.background_gradient to accept vmin vmax and dtype Int64

Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
@jbrockmendel jbrockmendel added ExtensionArray Extending pandas with custom dtypes or arrays. Styler conditional formatting using DataFrame.style labels Dec 18, 2019
proost pushed a commit to proost/pandas that referenced this issue Dec 19, 2019
…andas-dev#29245)

* ENH: Styler.background_gradient to accept vmin vmax and dtype Int64

Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
proost pushed a commit to proost/pandas that referenced this issue Dec 19, 2019
…andas-dev#29245)

* ENH: Styler.background_gradient to accept vmin vmax and dtype Int64

Resolve pandas-dev#12145 and pandas-dev#28869
For `vmin` and `vmax` use the same implementation in `Styler.bar`
For dtype `Int64` issue, deprecated `.values` and use `.to_numpy` instead
Here explicitly assign the dtype to float since we are doing normalize
@mroeschke mroeschke added the Bug label Apr 4, 2020
@mzeitlin11
Copy link
Member

Works now on master

@mzeitlin11 mzeitlin11 added Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Jan 2, 2021
@MarcoGorelli
Copy link
Member

Can confirm it's fixed now

@MarcoGorelli
Copy link
Member

@attack68 is there a test for this? If not, best to leave it open til then

@MarcoGorelli MarcoGorelli reopened this Feb 11, 2021
@attack68
Copy link
Contributor

@MarcoGorelli Isn't this the test integrated as part of the PR 29245 above:

def test_background_gradient_int64(self):
        # GH 28869
        df1 = pd.Series(range(3)).to_frame()
        df2 = pd.Series(range(3), dtype="Int64").to_frame()
        ctx1 = df1.style.background_gradient()._compute().ctx
        ctx2 = df2.style.background_gradient()._compute().ctx
        assert ctx2[(0, 0)] == ctx1[(0, 0)]
        assert ctx2[(1, 0)] == ctx1[(1, 0)]
        assert ctx2[(2, 0)] == ctx1[(2, 0)] 

That's why I though this was closeable?

@MarcoGorelli
Copy link
Member

@attack68 my bad, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. good first issue Needs Tests Unit test(s) needed to prevent regressions Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

6 participants