Skip to content

Commit

Permalink
allow missing values in columns (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
smason authored Jan 2, 2025
1 parent f6d803f commit 8c2fc78
Show file tree
Hide file tree
Showing 2 changed files with 30 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
20 changes: 20 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,21 @@ 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
0,1lb
1,
"""
)
df = pd.read_csv(io.StringIO(data), dtype=dict(mass="pint[kg]"))
mass = df["mass"]
assert mass.dtype == PintType("kg")

missing = pd.Series([False, True], name="mass")
tm.assert_equal(pd.isna(mass), missing)

0 comments on commit 8c2fc78

Please sign in to comment.