Search code examples
pythonpandaspint

Why am I getting a UndefinedUnitError using pint & pandas?


Following along with the documentation of pint-pandas and pint as best I could, I have an implementation which does not seem to want to work. This reproduces my UndefinedUnitError.

import pint
import pint_pandas
ureg = UnitRegistry()
ureg.load_definitions('pint_unit_definitions.txt')
pint.set_application_registry(ureg)


df = pd.DataFrame([[4,5,6],[1,3,4]], dtype='pint[bpm]' )

My pint_unit_definitions.txt file looks like this:

minute = 60 * second = min
beats_per_minute = beat / minute = bpm
hertz = counts / second = hz
beat = [heart_beats] = b

What am I doing wrong?

Thanks!


Solution

  • Andrew Savage kindly answered this question for me on github. It turns out I was missing a line. It works as below:

    import pint
    import pint_pandas
    ureg = UnitRegistry()
    ureg.load_definitions('pint_unit_definitions.txt')
    pint.set_application_registry(ureg)
    pint_pandas.PintType.ureg = ureg
    
    
    df = pd.DataFrame([[4,5,6],[1,3,4]], dtype='pint[bpm]' )