-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds aliasing of Column and ColumnSet via DefinedLater
- Loading branch information
Showing
15 changed files
with
304 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import datetime | ||
from pathlib import Path | ||
|
||
from pandandic import BaseFrame, Column, ColumnSet, ColumnGroup, DefinedLater | ||
|
||
|
||
class ExpertFrame(BaseFrame): | ||
""" | ||
Aliasing can be used to dynamically set columns or column set members at runtime. | ||
""" | ||
date = Column(type=datetime.date, alias=DefinedLater) | ||
metadata = ColumnSet(members=DefinedLater) | ||
|
||
temperature = ColumnSet(type=float, members=["temperature-\d+"], regex=True) | ||
door_open = ColumnSet(type=bool, members=["door-open-0", "door-open-1", "door-open-2"], regex=False) | ||
|
||
time_series = ColumnGroup(members=[temperature, door_open]) | ||
|
||
|
||
# anything DefinedLater MUST be set before ExpertFrame reads or accesses a Column or ColumnSet via attribute | ||
ExpertFrame.date.alias = "date" | ||
ExpertFrame.metadata.members = ["comment", "ref"] | ||
|
||
df = ExpertFrame().read_csv(Path(__file__).parent.joinpath("advanced.csv").as_posix()) | ||
df.set_index(ExpertFrame.date.column_name, inplace=True) # now sets index with the defined alias | ||
print(df.metadata) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pandandic" | ||
version = "0.1.1a0" | ||
version = "0.2.0" | ||
description = "A typed dataframe helper" | ||
license = "MIT" | ||
authors = ["Will Martin <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
from dataclasses import dataclass | ||
from typing import Type, Optional | ||
from typing import Type, Any, Optional, Union | ||
|
||
from .defined_later import DefinedLater | ||
|
||
|
||
@dataclass | ||
class Column: | ||
type: Type | ||
type: Type = Any | ||
alias: Optional[Union[str, DefinedLater, DefinedLater.__class__]] = None | ||
|
||
def __set_name__(self, _, name): | ||
self.name = name | ||
|
||
@property | ||
def column_name(self) -> Union[str, DefinedLater, DefinedLater.__class__]: | ||
return self.alias or self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .column import Column | ||
|
||
|
||
class ColumnAliasNotYetDefinedException(Exception): | ||
def __init__(self, column: Column): | ||
self._column = column | ||
|
||
def __str__(self) -> str: | ||
return f"Error. Column with name {self._column.name} was used with an alias that is not defined." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/pandandic/column_set_members_not_yet_defined_exception.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .column_set import ColumnSet | ||
|
||
|
||
class ColumnSetMembersNotYetDefinedException(Exception): | ||
def __init__(self, column_set: ColumnSet): | ||
self._column_set = column_set | ||
|
||
def __str__(self) -> str: | ||
return f"Error. ColumnSet with name {self._column_set.name} was used with a member list that is not defined." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class DefinedLater: | ||
""" | ||
Denotes that a Column alias or ColumnSet member list will be defined dynamically at runtime. | ||
If a read call is made before the DefinedLater is replaced, a ColumnAliasNotYetDefinedException or | ||
ColumnSetMembersNotYetDefinedException will be thrown. | ||
""" | ||
... |
Oops, something went wrong.