Skip to content

Commit

Permalink
allow missing values in columns
Browse files Browse the repository at this point in the history
partial fix for hgrecco#267

previously the code would fail with:

  DimensionalityError: Cannot convert from 'dimensionless' (dimensionless) to 'someunit' ([mass]).
  • Loading branch information
smason committed Jan 2, 2025
1 parent f6d803f commit 1244b39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pint_pandas/pint_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,16 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
def _from_sequence_of_strings(cls, scalars, dtype=None, copy=False):
if not dtype:
dtype = PintType.construct_from_quantity_string(scalars[0])
return cls._from_sequence([dtype.ureg.Quantity(x) for x in scalars], dtype)
# cache this lookup as it'll be used for every value
_Q = dtype.ureg.Quantity

def quantity(value):
# Pandas seems to pass empty strings as NaNs
if isinstance(value, float) and np.isnan(value):
return np.nan
return _Q(value)

return cls._from_sequence(list(map(quantity, scalars)), dtype)

@classmethod
def _from_factorized(cls, values, original):
Expand Down
16 changes: 16 additions & 0 deletions pint_pandas/testsuite/test_issues.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import io
import pickle
from textwrap import dedent
import time

import numpy as np
Expand Down Expand Up @@ -344,3 +346,17 @@ class TestIssue247(BaseExtensionTests):
result = a / a
expected = pd.Series([1, 1, 1], dtype="pint[][Float64]")
tm.assert_series_equal(result, expected)


class TestIssue267(BaseExtensionTests):
def test_missing_values(self):
"make sure that a missing values don't prevent the column from being imported"
data = dedent(
"""\
mass
1,1lb
2,
"""
)
df = pd.read_csv(io.StringIO(data), dtype=dict(mass="pint[kg]"))
assert df["mass"].dtype == PintType("kg")

0 comments on commit 1244b39

Please sign in to comment.