hgrecco / pint-pandas

Pandas support for pint
Other
169 stars 42 forks source link

UnitStrippedWarning, simple case #128

Closed rjplevin closed 1 year ago

rjplevin commented 2 years ago

Weird behavior using pandas 1.4.2, pint 0.19.2, pint-pandas 0.2.

I've seen many issues related to UnitStrippedWarning, but this is a simple case taken from the pint-pandas github page. It should work without a warning, right?

I'm getting these warnings whenever I create pd.Series with a pint dtype. Can anyone explain how to fix these other than ignoring the warnings?

import pandas as pd
import pint_pandas
s = pd.Series([1., 2., 2., 3.], dtype="pint[lbf ft]")
s
Out[5]: 
0    1.0
1    2.0
2    2.0
3    3.0
dtype: pint[foot * force_pound]
/Volumes/Plevin1TB/Software/anaconda3/envs/opgee/lib/python3.8/site-packages/pint_pandas/pint_array.py:648: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
  return np.array(qtys, dtype="object", copy=copy)

# Even stranger: every subsequent expression generates this warning

"anything"
Out[6]: 'anything'
/Volumes/Plevin1TB/Software/anaconda3/envs/opgee/lib/python3.8/site-packages/pint_pandas/pint_array.py:648: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
  return np.array(qtys, dtype="object", copy=copy)
MichaelTiemannOSC commented 2 years ago

This also happens to me when I pass a list of Series of type PintArrays to Pandas to make a DataFrame. Any insights would be welcome!

kenahoo commented 9 months ago

This still seems to be a problem, correct? I just generated the same error with the same code:

import pandas as pd
import pint_pandas

s = pd.Series([1., 2., 2., 3.], dtype="pint[lbf ft]")
s

Output:

Traceback (most recent call last):
  File "/Users/kwilliams/Library/Application Support/JetBrains/IntelliJIdea2023.2/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pandas/core/series.py", line 1685, in __repr__
    return self.to_string(**repr_params)
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pandas/core/series.py", line 1784, in to_string
    result = formatter.to_string()
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pandas/io/formats/format.py", line 393, in to_string
    fmt_values = self._get_formatted_values()
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pandas/io/formats/format.py", line 377, in _get_formatted_values
    return format_array(
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1296, in format_array
    return fmt_obj.get_result()
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1329, in get_result
    fmt_values = self._format_strings()
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1664, in _format_strings
    array = np.asarray(values)
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pint_pandas/pint_array.py", line 762, in __array__
    return self._to_array_of_quantity(copy=copy)
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pint_pandas/pint_array.py", line 775, in _to_array_of_quantity
    return np.array(qtys, dtype="object", copy=copy)
  File "/Users/kwilliams/.pyenv/versions/weather-extractor/lib/python3.9/site-packages/pint/facets/numpy/quantity.py", line 108, in __array__
    warnings.warn(
pint.errors.UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.

I'm using Pandas 2.1.1, Pint 0.22, Pint-Pandas 0.5.

MichaelTiemannOSC commented 9 months ago

A quick run in my environment doesn't reproduce.

(itr-test) michael@MDTMBP2020 ITR % python
Python 3.11.4 (v3.11.4:d2340ef257, Jun  6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import pint_pandas
>>> 
>>> s = pd.Series([1., 2., 2., 3.], dtype="pint[lbf ft]")
>>> s
0    1.0
1    2.0
2    2.0
3    3.0
dtype: pint[foot * force_pound]
>>> quit()
(itr-test) michael@MDTMBP2020 ITR % pip list | grep -i pint
Pint                          0.22
Pint-Pandas                   0.5
(itr-test) michael@MDTMBP2020 ITR % pip list | grep -i pandas
pandas                        2.1.2