Skip to content

Commit

Permalink
allowing eval between numerical values and PintArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Mutricy committed Jul 4, 2024
1 parent 5908e85 commit b650f07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,26 @@ def __repr__(self):
return self.name

def _get_common_dtype(self, dtypes):
if all(isinstance(x, PintType) for x in dtypes):
"""return the common dtype from list provided.
If this function is called this means at least on of the ``dtypes``
list is a ``PintType``
In order to be able to be able to perform operation on ``PintType``
with scalars, mix of ``PintType`` and numeric values are allowed.
Parameters
----------
dtypes (list): list of dtypes in which common is requested
Returns
-------
returns self for acceptable cases or None otherwise
"""
if all(
isinstance(x, PintType) or pd.api.types.is_numeric_dtype(x) for x in dtypes
):
return self
else:
return None
Expand Down
2 changes: 2 additions & 0 deletions pint_pandas/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ def test_eval(self):
{
"a": pd.Series([1.0, 2.0, 3.0], dtype="pint[meter]"),
"b": pd.Series([4.0, 5.0, 6.0], dtype="pint[second]"),
"c": [1.0, 2.0, 3.0],
}
)
tm.assert_series_equal(df.eval("a / b"), df["a"] / df["b"])
tm.assert_series_equal(df.eval("a / c"), df["a"] / df["c"])

0 comments on commit b650f07

Please sign in to comment.