Skip to content

Commit

Permalink
string types; release
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidcarbon committed Nov 20, 2024
1 parent ae2aadc commit d5ec14c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 11 additions & 1 deletion affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ def __init__(self, comment: str, *, value=None, array_class=pd.Series):
super().__init__(object, value, comment, array_class)


class ScalarString(Scalar):
def __init__(self, comment: str, *, value=None, array_class=pd.Series):
super().__init__(pd.StringDtype(), value, comment, array_class)


class ScalarBool(Scalar):
def __init__(self, comment: str, *, value=None, array_class=pd.Series):
super().__init__("boolean", value, comment, array_class)
Expand Down Expand Up @@ -497,9 +502,14 @@ def __init__(self, comment: str, *, values=None, array_class=pd.Series):
super().__init__(object, values, comment, array_class)


class VectorString(Vector):
def __init__(self, comment: str, *, values=None, array_class=pd.Series):
super().__init__(pd.StringDtype(), values, comment, array_class)


class VectorBool(Vector):
def __init__(self, comment: str, *, values=None, array_class=pd.Series):
super().__init__("boolean", values, comment, array_class)
super().__init__(np.bool, values, comment, array_class)


class VectorI8(Vector):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "affinity"
version = "0.7.0"
version = "0.8.0"
description = "Module for creating well-documented datasets, with types and annotations."
authors = [
{ name = "Alex Kislukhin" }
Expand Down
17 changes: 12 additions & 5 deletions test_affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

try:
import polars

NO_POLARS = False
except ImportError:
NO_POLARS = True

try:
import pyarrow

NO_PYARROW = False
except ImportError:
NO_PYARROW = True
Expand Down Expand Up @@ -54,10 +56,15 @@ def test_empty_vector():
def test_typed_descriptors():
s_untyped = af.ScalarObject("")
assert s_untyped.dtype == object
v_untyped = af.VectorObject("")
v_untyped = af.VectorObject("", values=[0, "0"])
assert v_untyped.dtype == object
v_bool = af.VectorBool("")
assert v_bool.dtype == "boolean"
assert v_untyped._values.values.tolist() == [0, "0"]
v_str = af.VectorString("", values=[0, "0"])
assert v_str.dtype == pd.StringDtype()
assert v_str._values.tolist() == ["0", "0"]
v_bool = af.VectorBool("", values=[False, 1])
assert v_bool.dtype == v_bool.dtype
assert v_bool._values.tolist() == [False, True]
v_i8 = af.VectorI8("")
assert v_i8.dtype == pd.Int8Dtype()
v_i16 = af.VectorI16("")
Expand Down Expand Up @@ -228,7 +235,7 @@ class aDataset(af.Dataset):
pd.testing.assert_frame_equal(data.df, data2.df)
assert data.origin.get("source") == "dataframe, shape (2, 3)"
default_dtypes = source_df.dtypes
desired_dtypes = {"v1": "boolean", "v2": np.float32, "v3": pd.Int16Dtype()}
desired_dtypes = {"v1": np.bool, "v2": np.float32, "v3": pd.Int16Dtype()}
pd.testing.assert_frame_equal(data.df, source_df.astype(desired_dtypes))
with pytest.raises(AssertionError):
pd.testing.assert_frame_equal(data.df, source_df.astype(default_dtypes))
Expand All @@ -252,7 +259,7 @@ class aDataset(af.Dataset):
data.origin.get("source") == "dataframe, shape (2, 3)\nquery:\nFROM source_df"
)
default_dtypes = source_df.dtypes
desired_dtypes = {"v1": "boolean", "v2": np.float32, "v3": pd.Int16Dtype()}
desired_dtypes = {"v1": np.bool, "v2": np.float32, "v3": pd.Int16Dtype()}
pd.testing.assert_frame_equal(data.df, source_df.astype(desired_dtypes))
with pytest.raises(AssertionError):
pd.testing.assert_frame_equal(data.df, source_df.astype(default_dtypes))
Expand Down

0 comments on commit d5ec14c

Please sign in to comment.