hgrecco / pint-pandas

Pandas support for pint
Other
169 stars 42 forks source link

Shortened form of dimensionless unit not recognised in `dtype = 'pint[unit]'` #127

Closed rwijtvliet closed 1 year ago

rwijtvliet commented 2 years ago

The shortened form of the units of a dimensionless quantity is the empty string. But "pint[]" is not recognized as a valid dtype when constructing a pandas.Series. See final line in example below.

import pint
import pandas
import pint_pandas
ureg = pint.UnitRegistry()
Q_ = ureg.Quantity

q1 = Q_(2.2, "seconds") # with physical unit.
q2 = Q_(1.1, "")  # dimensionless, e.g. a multiplication factor

# Working: standard form
ureg.default_format = ""
print(q1) # '2.2 second'
print(q2) # '1.1 dimensionless'
# Stretch into Series is possible
q1_as_series = pandas.Series(q1.magnitude, range(3), dtype=f'pint[{q1.units}]') #ok
q2_as_series = pandas.Series(q2.magnitude, range(3), dtype=f'pint[{q2.units}]') #ok

# Not working: shortened form
ureg.default_format = "~P"  # short by default
print(q1) # '2.2 s'
print(q2) # '1.1'
# Stretch into Series is only possible for q1
q1_as_series = pandas.Series(q1.magnitude, range(3), dtype=f'pint[{q1.units}]') #ok
q2_as_series = pandas.Series(q2.magnitude, range(3), dtype=f'pint[{q2.units}]') #not ok!
# UndefinedUnitError: 'pint[]' is not defined in the unit registry

If this is an issue specifically for pint_pandas, I can also file the bugreport there.

rwijtvliet commented 2 years ago

If there is a work-around, I'd love to hear it. I currently have

def pintunit(q) -> str:
    units = f"{q.units}" or 'dimensionless'
    return f"pint[{units}]"

q2_as_series = pandas.Series(q2.magnitude, range(3), dtype=pintunit(q2)) # ok