Skip to content

Commit

Permalink
Fix issue where quantify doesn't work on DataFrames containing string…
Browse files Browse the repository at this point in the history
… columns (#217)

* update values to iloc to avoid casting array to obj type

* add test for PR217

* run pre-commit tests to pass lint test

---------

Co-authored-by: Nicholas Hemenway <nhemenway@mercmarine.com>
  • Loading branch information
Nick-Hemenway and nhemenway-work committed Apr 3, 2024
1 parent f9d3a66 commit 6646cfa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def quantify(self, level=-1):

df_new = DataFrame(
{
i: PintArray(df.values[:, i], unit)
i: PintArray(df.iloc[:, i], unit)
if unit != NO_UNIT
else df.values[:, i]
for i, unit in enumerate(units.values)
Expand Down
13 changes: 13 additions & 0 deletions pint_pandas/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,16 @@ def test_dequantify(self):
)
result = df.pint.dequantify()
tm.assert_frame_equal(expected, result)


class TestIssue217(BaseExtensionTests):
def test_roundtrip(self):
df = pd.DataFrame(
{
"power": pd.Series([1.0, 2.0, 3.0], dtype="pint[W]"),
"torque": pd.Series([4.0, 5.0, 6.0], dtype="pint[N*m]"),
"fruits": pd.Series(["apple", "pear", "kiwi"]),
}
)
df1 = df.pint.dequantify().pint.quantify(level=-1)
tm.assert_equal(df1.power.pint.m, df.power.pint.m)

0 comments on commit 6646cfa

Please sign in to comment.