Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow missing values in columns #268

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading