Closed mmarras closed 1 year ago
I guess something like this would do:
_df = pandas.DataFrame.from_dict(mydict)
for col in _df:
try:
unit = str(_df[col].values[0].u)
_df[col] = _df[col].astype(f'pint[{unit}]')
except AttributeError: # if it has no unit then dont recast
pass
# to check
_df.pint.dequantify()
This PR would do what you want https://github.com/hgrecco/pint-pandas/pull/58
Awesome, any timeline on when that becomes available? Is there a roadblock?
I've been using
pint
for a while and as an effect, I get lots of calculation results of typepint.Quantity
.Working with pint-pandas works nicely, with having quantities on column level. However, how would I convert element level quantities to a column level quanitity?
Imagine I have a dictionary which is the result of a calculation:
if I cast this into a
Dataframe
viapd.Dataframe.from_dict(myresults)
I get the units attached to the elements of the table. But not to the column. Now if I had the results for another set of input parameters and would add that to thedf
as a new row, then for me it would make sense to have a functionality that can convert to column-wide units (of course given that the units are convertable into each other). As a default it could useto_base_units
if the units are not identical for each element of the column.Does this exist? Does it make sense?