From ccee8658e236b245e44439313ae988b2ff372382 Mon Sep 17 00:00:00 2001 From: thaeber <40823270+thaeber@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:10:42 +0200 Subject: [PATCH 1/3] Using iloc to avoid casting array to obj type for columns without units --- pint_pandas/pint_array.py | 2 +- pint_pandas/testsuite/test_issues.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index 06df5de..bd330d1 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -969,7 +969,7 @@ def quantify(self, level=-1): { i: PintArray(df.iloc[:, i], unit) if unit != NO_UNIT - else df.values[:, i] + else df.iloc[:, 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 e3ff933..c4ae247 100644 --- a/pint_pandas/testsuite/test_issues.py +++ b/pint_pandas/testsuite/test_issues.py @@ -242,3 +242,18 @@ def test_roundtrip(self): ) df1 = df.pint.dequantify().pint.quantify(level=-1) tm.assert_equal(df1.power.pint.m, df.power.pint.m) + + +class TestIssue218(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"]), + "float_numbers": pd.Series([1.0, 2.0, 3.0], dtype="float64"), + "int_numbers": pd.Series([1.0, 2.0, 3.0], dtype="int"), + } + ) + df1 = df.pint.dequantify().pint.quantify(level=-1) + tm.assert_equal(df1, df, check_dtype=True) From 9f28e79d8c62991941dce5e690b16d9be697c335 Mon Sep 17 00:00:00 2001 From: thaeber <40823270+thaeber@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:23:29 +0200 Subject: [PATCH 2/3] run pre-commit tests --- pint_pandas/pint_array.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pint_pandas/pint_array.py b/pint_pandas/pint_array.py index bd330d1..4366816 100644 --- a/pint_pandas/pint_array.py +++ b/pint_pandas/pint_array.py @@ -967,9 +967,7 @@ def quantify(self, level=-1): df_new = DataFrame( { - i: PintArray(df.iloc[:, i], unit) - if unit != NO_UNIT - else df.iloc[:, i] + i: PintArray(df.iloc[:, i], unit) if unit != NO_UNIT else df.iloc[:, i] for i, unit in enumerate(units.values) } ) From 14cd71995ac72154c83f06c6763083d231761537 Mon Sep 17 00:00:00 2001 From: thaeber <40823270+thaeber@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:32:08 +0200 Subject: [PATCH 3/3] Changed name of test class to match # of PR --- pint_pandas/testsuite/test_issues.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint_pandas/testsuite/test_issues.py b/pint_pandas/testsuite/test_issues.py index c4ae247..9032670 100644 --- a/pint_pandas/testsuite/test_issues.py +++ b/pint_pandas/testsuite/test_issues.py @@ -244,7 +244,7 @@ def test_roundtrip(self): tm.assert_equal(df1.power.pint.m, df.power.pint.m) -class TestIssue218(BaseExtensionTests): +class TestIssue225(BaseExtensionTests): def test_roundtrip(self): df = pd.DataFrame( {