getml.data.View
View(
base: Union[DataFrame, View],
name: Optional[str] = None,
subselection: Optional[
Union[
BooleanColumnView, FloatColumn, FloatColumnView
]
] = None,
added: Optional[Dict] = None,
dropped: Optional[List[str]] = None,
)
A view is a lazily evaluated, immutable representation of a DataFrame
.
There are important differences between a DataFrame
and a view:
-
Views are lazily evaluated. That means that views do not contain any data themselves. Instead, they just refer to an underlying data frame. If the underlying data frame changes, so will the view (but such behavior will result in a warning).
-
Views are immutable. In-place operations on a view are not possible. Any operation on a view will result in a new view.
-
Views have no direct representation on the getML Engine, and therefore they do not need to have an identifying name.
ATTRIBUTE | DESCRIPTION |
---|---|
base | A data frame or view used as the basis for this view. |
name | The name assigned to this view. TYPE: |
subselection | Indicates which rows we would like to keep. TYPE: |
added | A dictionary that describes a new column that has been added to the view. TYPE: |
dropped | A list of columns that have been dropped. |
Example
You hardly ever directly create views. Instead, it is more likely that you will encounter them as a result of some operation on a DataFrame
:
# Creates a view on the first 100 lines
view1 = data_frame[:100]
# Creates a view without some columns.
view2 = data_frame.drop(["col1", "col2"])
# Creates a view in which some roles are reassigned.
view3 = data_frame.with_role(["col1", "col2"], getml.data.roles.categorical)
# Assign baseline roles
data_frame.set_role(["jk"], getml.data.roles.join_key)
data_frame.set_role(["col1", "col2"], getml.data.roles.categorical)
data_frame.set_role(["col3", "col4"], getml.data.roles.numerical)
data_frame.set_role(["col5"], getml.data.roles.target)
# Make the data frame immutable, so in-place operations are
# no longer possible.
data_frame.freeze()
# Save the data frame.
data_frame.save()
# I suspect that col1 leads to overfitting, so I will drop it.
view = data_frame.drop(["col1"])
# Insert the view into a container.
container = getml.data.Container(...)
container.add(some_alias=view)
container.save()
The advantage of using such a pattern is that it enables you to always completely retrace your entire pipeline without creating deep copies of the data frames whenever you have made a small change like the one in our example. Note that the pipeline will record which Container
you have used.
Source code in getml/data/view.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
added property
added: Dict
The column that has been added to the view.
RETURNS | DESCRIPTION |
---|---|
Dict | The column that has been added to the view. |
base property
colnames property
columns property
dropped property
last_change property
last_change: str
A string describing the last time this data frame has been changed.
RETURNS | DESCRIPTION |
---|---|
str | A string describing the last time this data frame has been changed. |
name property
name: str
The name of the view. If no name is explicitly set, the name will be identical to the name of the base.
RETURNS | DESCRIPTION |
---|---|
str | The name of the view. |
roles property
roles: Roles
The roles of the columns included in this View.
RETURNS | DESCRIPTION |
---|---|
Roles | The roles of the columns included in this View. |
rowid property
subselection property
subselection: Union[
BooleanColumnView, FloatColumn, FloatColumnView
]
The subselection that is applied to this view.
RETURNS | DESCRIPTION |
---|---|
Union[BooleanColumnView, FloatColumn, FloatColumnView] | The subselection that is applied to this view. |
shape property
A tuple containing the number of rows and columns of the View.
check
check()
Checks whether the underlying data frame has been changed after the creation of the view.
Source code in getml/data/view.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
drop
Returns a new View
that has one or several columns removed.
PARAMETER | DESCRIPTION |
---|---|
cols | The names of the columns to be dropped. |
RETURNS | DESCRIPTION |
---|---|
View | A new view with the specified columns removed. |
Source code in getml/data/view.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
|
ncols
ncols() -> int
Number of columns in the current instance.
RETURNS | DESCRIPTION |
---|---|
int | Overall number of columns |
Source code in getml/data/view.py
485 486 487 488 489 490 491 492 |
|
nrows
Returns the number of rows in the current instance.
PARAMETER | DESCRIPTION |
---|---|
force | If the number of rows is unknown, do you want to force the Engine to calculate it anyway? This is a relatively expensive operation, therefore you might not necessarily want this. TYPE: |
RETURNS | DESCRIPTION |
---|---|
Union[int, str] | The number of rows in the current instance. |
Source code in getml/data/view.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
|
refresh
refresh() -> View
Aligns meta-information of the current instance with the corresponding data frame in the getML Engine.
RETURNS | DESCRIPTION |
---|---|
View | Updated handle the underlying data frame in the getML |
View | Engine. |
Source code in getml/data/view.py
548 549 550 551 552 553 554 555 556 557 558 |
|
to_arrow
to_arrow() -> Table
Creates a pyarrow.Table
from the view.
Loads the underlying data from the getML Engine and constructs a pyarrow.Table
.
RETURNS | DESCRIPTION |
---|---|
Table | Pyarrow equivalent of the current instance including |
Table | its underlying data. |
Source code in getml/data/view.py
636 637 638 639 640 641 642 643 644 645 646 |
|
to_json
to_json() -> str
Creates a JSON string from the current instance.
Loads the underlying data from the getML Engine and constructs a JSON string.
RETURNS | DESCRIPTION |
---|---|
str | JSON string of the current instance including its |
str | underlying data. |
Source code in getml/data/view.py
650 651 652 653 654 655 656 657 658 659 660 |
|
to_csv
to_csv(
fname: str,
quotechar: str = '"',
sep: str = ",",
batch_size: int = 0,
quoting_style: str = "needed",
)
Writes the underlying data into a newly created CSV file.
PARAMETER | DESCRIPTION |
---|---|
fname | The name of the CSV file. The ending ".csv" and an optional batch number will be added automatically. TYPE: |
quotechar | The character used to wrap strings. TYPE: |
sep | The character used for separating fields. TYPE: |
batch_size | Maximum number of lines per file. Set to 0 to read the entire data frame into a single file. TYPE: |
quoting_style | The quoting style to use. Delegated to pyarrow. The following values are accepted: - TYPE: |
Deprecated
1.5: The quotechar
parameter is deprecated.
Source code in getml/data/view.py
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
|
to_db
to_db(table_name: str, conn: Optional[Connection] = None)
Writes the underlying data into a newly created table in the database.
PARAMETER | DESCRIPTION |
---|---|
table_name | Name of the table to be created. If a table of that name already exists, it will be replaced. TYPE: |
conn | The database connection to be used. If you don't explicitly pass a connection, the Engine will use the default connection. TYPE: |
Source code in getml/data/view.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
to_df
to_df(name) -> DataFrame
Creates a DataFrame
from the view.
Source code in getml/data/view.py
756 757 758 759 760 761 |
|
to_pandas
to_pandas() -> DataFrame
Creates a pandas.DataFrame
from the view.
Loads the underlying data from the getML Engine and constructs a pandas.DataFrame
.
RETURNS | DESCRIPTION |
---|---|
DataFrame | Pandas equivalent of the current instance including |
DataFrame | its underlying data. |
Source code in getml/data/view.py
765 766 767 768 769 770 771 772 773 774 775 |
|
to_placeholder
to_placeholder(name: Optional[str] = None) -> Placeholder
Generates a Placeholder
from the current View
.
PARAMETER | DESCRIPTION |
---|---|
name | The name of the placeholder. If no name is passed, then the name of the placeholder will be identical to the name of the current view. |
RETURNS | DESCRIPTION |
---|---|
Placeholder | A placeholder with the same name as this data frame. |
Source code in getml/data/view.py
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 |
|
to_parquet
to_parquet(
fname: str,
compression: Literal[
"brotli", "gzip", "lz4", "snappy", "zstd"
] = "snappy",
)
Writes the underlying data into a newly created parquet file.
PARAMETER | DESCRIPTION |
---|---|
fname | The name of the parquet file. The ending ".parquet" will be added automatically. TYPE: |
compression | The compression format to use. Supported values are "brotli", "gzip", "lz4", "snappy", "zstd" TYPE: |
Source code in getml/data/view.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
|
to_pyspark
Creates a pyspark.sql.DataFrame
from the current instance.
Loads the underlying data from the getML Engine and constructs a pyspark.sql.DataFrame
.
PARAMETER | DESCRIPTION |
---|---|
spark | The pyspark session in which you want to create the data frame. TYPE: |
name | The name of the temporary view to be created on top of the |
RETURNS | DESCRIPTION |
---|---|
DataFrame | Pyspark equivalent of the current instance including |
DataFrame | its underlying data. |
Source code in getml/data/view.py
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 |
|
to_s3
Writes the underlying data into a newly created CSV file located in an S3 bucket.
Note
S3 is not supported on Windows.
PARAMETER | DESCRIPTION |
---|---|
bucket | The bucket from which to read the files. TYPE: |
key | The key in the S3 bucket in which you want to write the output. The ending ".csv" and an optional batch number will be added automatically. TYPE: |
region | The region in which the bucket is located. TYPE: |
sep | The character used for separating fields. TYPE: |
batch_size | Maximum number of lines per file. Set to 0 to read the entire data frame into a single file. TYPE: |
Example
getml.engine.set_s3_access_key_id("YOUR-ACCESS-KEY-ID")
getml.engine.set_s3_secret_access_key("YOUR-SECRET-ACCESS-KEY")
your_view.to_s3(
bucket="your-bucket-name",
key="filename-on-s3",
region="us-east-2",
sep=';'
)
Source code in getml/data/view.py
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
|
where
where(
index: Optional[
Union[
BooleanColumnView, FloatColumn, FloatColumnView
]
]
) -> View
Extract a subset of rows.
Creates a new View
as a subselection of the current instance.
PARAMETER | DESCRIPTION |
---|---|
index | Boolean column indicating the rows you want to select. TYPE: |
RETURNS | DESCRIPTION |
---|---|
View | A new view containing only the rows that satisfy the condition. |
Example
Generate example data:
data = dict(
fruit=["banana", "apple", "cherry", "cherry", "melon", "pineapple"],
price=[2.4, 3.0, 1.2, 1.4, 3.4, 3.4],
join_key=["0", "1", "2", "2", "3", "3"])
fruits = getml.DataFrame.from_dict(data, name="fruits",
roles={"categorical": ["fruit"], "join_key": ["join_key"], "numerical": ["price"]})
fruits
| join_key | fruit | price |
| join key | categorical | numerical |
--------------------------------------
| 0 | banana | 2.4 |
| 1 | apple | 3 |
| 2 | cherry | 1.2 |
| 2 | cherry | 1.4 |
| 3 | melon | 3.4 |
| 3 | pineapple | 3.4 |
cherries = fruits.where(
fruits["fruit"] == "cherry")
cherries
| join_key | fruit | price |
| join key | categorical | numerical |
--------------------------------------
| 2 | cherry | 1.2 |
| 2 | cherry | 1.4 |
Source code in getml/data/view.py
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
|
with_column
with_column(
col: Union[
bool,
str,
float,
int,
datetime64,
FloatColumn,
FloatColumnView,
StringColumn,
StringColumnView,
BooleanColumnView,
],
name: str,
role: Optional[Role] = None,
unit: str = "",
subroles: Optional[
Union[Subrole, Iterable[str]]
] = None,
time_formats: Optional[List[str]] = None,
) -> View
Returns a new View
that contains an additional column.
PARAMETER | DESCRIPTION |
---|---|
col | The column to be added. TYPE: |
name | Name of the new column. TYPE: |
role | Role of the new column. Must be from |
subroles | Subroles of the new column. Must be from |
unit | Unit of the column. TYPE: |
time_formats | Formats to be used to parse the time stamps. This is only necessary, if an implicit conversion from a The formats are allowed to contain the following special characters:
|
RETURNS | DESCRIPTION |
---|---|
View | A new view containing the additional column. |
Source code in getml/data/view.py
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 |
|
with_name
Returns a new View
with a new name.
PARAMETER | DESCRIPTION |
---|---|
name | The name of the new view. TYPE: |
RETURNS | DESCRIPTION |
---|---|
View | A new view with the new name. |
Source code in getml/data/view.py
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 |
|
with_role
with_role(
names: Union[str, List[str]],
role: str,
time_formats: Optional[List[str]] = None,
) -> View
Returns a new View
with modified roles.
When switching from a role based on type float to a role based on type string or vice verse, an implicit type conversion will be conducted. The time_formats
argument is used to interpret time format string: annotating_roles_time_stamp
. For more information on roles, please refer to the User Guide.
PARAMETER | DESCRIPTION |
---|---|
names | The name or names of the column. |
role | The role to be assigned. TYPE: |
time_formats | Formats to be used to parse the time stamps. This is only necessary, if an implicit conversion from a StringColumn to a time stamp is taking place. |
RETURNS | DESCRIPTION |
---|---|
View | A new view with the modified roles. |
Source code in getml/data/view.py
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 |
|
with_subroles
with_subroles(
names: Union[str, List[str]],
subroles: Union[Subrole, Iterable[str]],
append: bool = True,
) -> View
Returns a new view with one or several new subroles on one or more columns.
PARAMETER | DESCRIPTION |
---|---|
names | The name or names of the column. |
subroles | The subroles to be assigned. |
append | Whether you want to append the new subroles to the existing subroles. TYPE: |
RETURNS | DESCRIPTION |
---|---|
View | A new view with the modified subroles. |
Source code in getml/data/view.py
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 |
|
with_unit
Returns a view that contains a new unit on one or more columns.
PARAMETER | DESCRIPTION |
---|---|
names | The name or names of the column. |
unit | The unit to be assigned. TYPE: |
comparison_only | Whether you want the column to be used for comparison only. This means that the column can only be used in comparison to other columns of the same unit. An example might be a bank account number: The number in itself is hardly interesting, but it might be useful to know how often we have seen that same bank account number in another table. If True, this will append ", comparison only" to the unit. The feature learning algorithms and the feature selectors will interpret this accordingly. TYPE: |
RETURNS | DESCRIPTION |
---|---|
View | A new view with the modified unit. |
Source code in getml/data/view.py
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 |
|