You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried adding a pint.Quantity to an existing pd.DataFrame, thinking that pint-pandas might transform the Quantity into a PintArray that:
matches the number of rows
preserves the unit.
Reproducible Example
importpandasaspdfrompint_pandasimportPintArray, Quantity# Existing pd.DataFramedata= {"bar": [0.07, 0.30, 0.85, 1.00]}
df=pd.DataFrame(data)
# Trying to add a `pint.Quantity`df['content'] =Quantity(42.0, units='percent')
df.dtypes# Output:# bar float64# content object # <---- Expected: pint[percent]
output:
bar
content
0
0.07
42.0 percent
1
0.30
42.0 percent
2
0.85
42.0 percent
3
1.00
42.0 percent
Question
The documentation indeed suggests using a pd.Series or a PintArray to achieve this, but this approach feels a bit verbose. I’d like to add a new column directly with just Quantity to mimic the pandas API when creating a new column from a float to an existing pd.DataFrame.
Would it make sense to convert a pint.Quantity into a PintArray when adding it to a pd.DataFrame?
The text was updated successfully, but these errors were encountered:
An ExtensionArray must have a defined length, making your suggestion not possible.
Yes, it would be very nice to have. To get this work, pandas needs to identify the quantity as a scalar of PintType, then try to construct a PintArray. pandas-dev/pandas#27995
Description
I tried adding a
pint.Quantity
to an existingpd.DataFrame
, thinking thatpint-pandas
might transform theQuantity
into aPintArray
that:Reproducible Example
output:
Question
The documentation indeed suggests using a
pd.Series
or aPintArray
to achieve this, but this approach feels a bit verbose. I’d like to add a new column directly with justQuantity
to mimic the pandas API when creating a new column from afloat
to an existingpd.DataFrame
.Would it make sense to convert a
pint.Quantity
into aPintArray
when adding it to apd.DataFrame
?The text was updated successfully, but these errors were encountered: