Closed MichaelTiemannOSC closed 1 year ago
Answering my own question, the gentle solution is to know that when switching registries, it is not enough to merely re-initialize Quantity, but other things must be fixed. This code from pint/pint/init.py shows what:
# Default Quantity, Unit and Measurement are the ones
# build in the default registry.
Quantity = UnitRegistry.Quantity
Unit = UnitRegistry.Unit
Measurement = UnitRegistry.Measurement
Context = UnitRegistry.Context
I've begun doing exploratory surgery on Pint-Pandas to see how ducky things are with uncertainties. I know that the "right" way to do this is to create some new extension types, but I'm exploring some quick-end-dirty things and have already found a few cases where More Work Will Be Needed (such as pd.DataFrame.rolling, which doesn't support ExtensionArrays at all).
I'm looking to add two PintArrays together, and down in
arithmetic_op
, as expected, should_extension_dispatch of my two operands is true, and there's a call:left
andright
have the expected dtypes:So far so good. Down in _binop (within the _create_method) function we see that
lvalues
andrvalues
have slightly different dtypes (they are not wrapped inpint{ ]
):I remember reading that PintArrays are special things that can deal with units not wrapped in
pint[ ]
. However...when we go to initialize the PintArray,dtype
comes in as<Unit('CO2e * kt / gigawatt_hour')>
. This is not seen as a PintType, so an attempt is made to construct one (PintType(dtype)
). The new allocator of course does not find dtype (which is calledunits
in this scope) as an instance of PintType (it's what we are construction) and it doesn't find it as an instance of _Unit. Thecls._parse_dtype_strict(units)
call fails because units is not astr
, it's a<class 'pint.util.Unit'>
. But it would be happy to construct a PintType for me out of the name of units, with or without thepint[ ]
decoration, just not the type that it is.I'd love to find a way to fix this gently. Thoughts?