Skip to content

Handle list and dict types #12

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions tests/test_pandas_typedframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,23 @@ def test_multiple_inheritance_2_failure():
def test_multiple_inheritance_2_failure_with_root_overwrite():
with pytest.raises(AssertionError):
_ = Down(pd.DataFrame({'root': [True], 'left': [True], 'right': ['string']}))


class ListFrame(TypedDataFrame):
schema = {
'col1': list
}


def test_convert_list_attribute_type():
_ = ListFrame.convert(pd.DataFrame({'col1': [["this"]]}))


class DictFrame(TypedDataFrame):
schema = {
'col1': dict
}


def test_convert_dict_attribute_type():
_ = DictFrame.convert(pd.DataFrame({'col1': [{"this": "that"}]}))
2 changes: 1 addition & 1 deletion tests/test_polars_typedframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_base_success_case():
'datetime_field': [datetime.datetime(2021, 5, 31, 12, 0, 0), datetime.datetime(2021, 6, 1, 12, 0, 0), datetime.datetime(2021, 6, 2, 12, 0, 0)],
'mixin_field': [1, 2, 3],
'new_field': [1, 2, 3]})
df = df.with_column(pl.col('int_field').cast(pl.Int16))
df = df.with_columns(pl.col('int_field').cast(pl.Int16))
_ = ChildDataFrame(df)


Expand Down
2 changes: 2 additions & 0 deletions typedframe/pandas_.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def convert(cls: Type[T], df: pd.DataFrame, add_optional_cols: bool = True) -> T
if categories_diff:
raise AssertionError(f"For column: {col} there are unknown categories: {categories_diff}")
df[col] = pd.Categorical(df[col], categories=expected[col], ordered=True)
elif expected[col] in (list, dict):
df[col] = expected[col](df[col])
elif expected[col] == DATE_TIME_DTYPE:
df[col] = pd.to_datetime(df[col])
elif expected[col] == UTC_DATE_TIME_DTYPE:
Expand Down