From 6646cfaeacd21cae839fc3b8f5842233b285c905 Mon Sep 17 00:00:00 2001 From: Nick-Hemenway <47535659+Nick-Hemenway@users.noreply.github.com> Date: Wed, 3 Apr 2024 08:15:51 -0500 Subject: [PATCH] Fix issue where quantify doesn't work on DataFrames containing string 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 --- pint_pandas/pint_array.py | 2 +- pint_pandas/testsuite/test_issues.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index b10372c..06df5de 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -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) diff --git a/pint_pandas/testsuite/test_issues.py b/pint_pandas/testsuite/test_issues.py index fbcd0c6..e3ff933 100644 --- a/pint_pandas/testsuite/test_issues.py +++ b/pint_pandas/testsuite/test_issues.py @@ -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)