Loading...

The Magic Methods of the vDataFrame

VerticaPy 0.3.2 introduces the Magic methods, which offer some additional flexibility for mathematical operations in the vDataFrame. These methods let you handle many operations in a pandas-like or Pythonic style.

from verticapy.datasets import load_titanic

titanic = load_titanic()
titanic.head(100)
123
pclass
Int
100%
...
123
body
Int
9%
Abc
Varchar(100)
57%
11...22
21...[null]
31...[null]
41...[null]
51...[null]
61...[null]
71...[null]
81...62
91...[null]
101...[null]
111...[null]
121...[null]
131...110
141...[null]
151...[null]
161...38
171...[null]
181...126
191...292
201...175
211...[null]
221...122
231...166
241...[null]
251...207
261...[null]
271...232
281...[null]
291...[null]
301...[null]
311...[null]
321...[null]
331...46
341...169
351...[null]
361...[null]
371...[null]
381...[null]
391...[null]
401...[null]
411...[null]
421...[null]
431...[null]
441...[null]
451...[null]
461...[null]
471...[null]
481...[null]
491...[null]
501...[null]
511...[null]
521...[null]
531...[null]
541...[null]
551...[null]
561...[null]
571...[null]
581...[null]
591...[null]
601...[null]
611...[null]
621...[null]
631...[null]
641...[null]
651...[null]
661...[null]
671...[null]
681...[null]
691...[null]
701...[null]
711...[null]
721...[null]
731...[null]
742...[null]
752...[null]
762...[null]
772...[null]
782...[null]
792...[null]
802...[null]
812...[null]
822...[null]
832...236
842...[null]
852...[null]
862...[null]
872...155
882...[null]
892...75
902...35
912...[null]
922...[null]
932...[null]
942...[null]
952...[null]
962...[null]
972...[null]
982...165
992...[null]
1002...[null]

Feature Engineering, pandas-style

You can create new features with in a pandas style.

titanic["family_size"] = titanic["parch"] + titanic["sibsp"] + 1
titanic[["sibsp", "parch", "family_size"]]
123
sibsp
Integer
100%
...
123
parch
Integer
100%
123
family_size
Integer
100%
10...01
20...01
30...01
40...01
51...02
61...13
71...02
81...02
90...01
101...02
110...01
121...02
130...01
140...01
150...01
161...02
170...01
180...01
190...01
200...01

You can also create features from various mathematical functions.

import verticapy.sql.functions as fun

titanic["ln_fare"] = fun.ln(titanic["fare"])
titanic[["fare", "ln_fare"]]
123
fare
Numeric(12)
99%
123
ln_fare
Float(22)
99%
149.50423.90205751446006
235.53.56953269648137
331.03.43398720448515
447.13.85227300102237
5136.77924.91836794684454
683.15834.42074602026042
752.03.95124371858143
8106.4254.66744051171422
931.67923.45566031410212
1053.13.97217692824789
1179.24.37197629882038
1283.4754.42454718582972
130.0-inf
1442.53.74950407593037
1551.86253.94859598565899
1652.03.95124371858143
1730.69583.42412583741652
1830.03.40119738166216
1975.24174.32070559854228
2051.86253.94859598565899
titanic["x"] = 1 - fun.exp(-titanic["fare"])
titanic[["fare", "x"]]
123
fare
Numeric(12)
99%
123
x
Float(22)
99%
149.50421.0
235.51.0
331.00.999999999999966
447.11.0
5136.77921.0
683.15831.0
752.01.0
8106.4251.0
931.67920.999999999999983
1053.11.0
1179.21.0
1283.4751.0
130.00.0
1442.51.0
1551.86251.0
1652.01.0
1730.69580.999999999999953
1830.00.999999999999906
1975.24171.0
2051.86251.0

Conditional Operators

You can now filter your data with conditional operators like and (&), or (|), equals (==), not equals (!=), and more!

Equal Operator (==)

# Identifies the passengers who came alone

single_family = titanic[titanic["family_size"] == 1]
single_family.head(100)
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
99%
123
x
Float(22)
99%
11...3.902057514460061.0
21...3.569532696481371.0
31...3.433987204485150.999999999999966
41...3.852273001022371.0
51...3.455660314102120.999999999999983
61...4.371976298820381.0
71...-inf0.0
81...3.749504075930371.0
91...3.948595985658991.0
101...3.424125837416520.999999999999953
111...3.401197381662160.999999999999906
121...4.320705598542281.0
131...3.948595985658991.0
141...3.747148362237911.0
151...3.34990408727460.999999999999581
161...3.951243718581431.0
171...-inf0.0
181...4.909954920578881.0
191...3.921890164505811.0
201...3.424125837416520.999999999999953
211...3.47571098582180.999999999999991
221...4.334564151246191.0
231...3.279029747687950.999999999997052
241...3.322183033934120.999999999999086
251...4.901564199041891.0
261...3.312213150784530.999999999998797
271...4.708478296903481.0
281...5.354224998486331.0
291...4.340282339965541.0
301...4.420746020260421.0
311...4.690430029938911.0
321...3.417726683613370.999999999999943
331...4.460144413937831.0
341...4.371976298820381.0
351...6.238967387173661.0
362...2.351375257163480.99997246355065
372...3.258096538021480.999999999994891
382...4.297285406218791.0
392...4.297285406218791.0
402...3.258096538021480.999999999994891
412...2.564949357461540.999997739670593
422...2.564949357461540.999997739670593
432...2.564949357461540.999997739670593
442...2.564949357461540.999997739670593
452...2.564949357461540.999997739670593
462...2.564949357461540.999997739670593
472...4.297285406218791.0
482...2.513656063073990.999995670246734
492...2.351375257163480.99997246355065
502...2.55528744654980.999997438711229
512...2.745936691242350.999999828588716
522...2.772588722239780.999999887464825
532...2.564949357461540.999997739670593
542...2.351375257163480.99997246355065
552...2.564949357461540.999997739670593
562...2.351375257163480.99997246355065
572...2.711098882480230.999999707792012
582...2.564949357461540.999997739670593
592...2.564949357461540.999997739670593
602...2.4849066497880.999993855787647
612...2.351375257163480.99997246355065
622...2.564949357461540.999997739670593
632...2.513656063073990.999995670246734
642...3.044522437723420.999999999241744
652...3.044522437723420.999999999241744
662...2.756840365271640.999999855501975
673...2.085672091430470.999680898077519
683...3.11462580349440.999999999834988
693...2.05091345806530.999579892530644
703...2.085672091430470.999680898077519
713...2.05091345806530.999579892530644
723...1.978128379979350.999274899288251
733...2.047692843365260.999569257459424
743...2.061048420562940.999611881557209
753...2.047692843365260.999569257459424
763...2.045535679650040.999562003658341
773...2.085672091430470.999680898077519
783...1.953027616824180.999132591042693
793...2.045535679650040.999562003658341
803...2.031982990520130.99951395045752
813...2.085672091430470.999680898077519
823...2.085672091430470.999680898077519
833...1.978128379979350.999274899288251
843...2.047692843365260.999569257459424
853...2.047692843365260.999569257459424
863...2.066330972529740.99962769606213
873...2.05091345806530.999579892530644
883...2.047692843365260.999569257459424
893...2.064226375873360.999621464235705
903...2.047692843365260.999569257459424
913...2.047692843365260.999569257459424
923...2.060513531794320.999610248031747
933...1.953027616824180.999132591042693
943...2.123757425454350.999766540046286
953...2.05091345806530.999579892530644
963...2.180417459019840.999856618263724
973...2.061048420562940.999611881557209
983...2.021547563260930.999473889872884
993...2.933191755079090.999999993070651
1003...2.070022319763340.99963841015017

And Operator (&)

# Identifies the passengers who came alone and
# who are between 15 and 24 years old...
# ...with comparison operators
single_family[(titanic["age"] >= 15) & (titanic["age"] <= 24)]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
100%
123
x
Float(22)
100%
11...3.852273001022371.0
21...4.371976298820381.0
31...4.909954920578881.0
41...4.420746020260421.0
52...2.351375257163480.99997246355065
62...4.297285406218791.0
72...4.297285406218791.0
82...2.564949357461540.999997739670593
92...2.564949357461540.999997739670593
102...4.297285406218791.0
112...2.351375257163480.99997246355065
122...2.564949357461540.999997739670593
132...2.4849066497880.999993855787647
142...2.351375257163480.99997246355065
153...2.085672091430470.999680898077519
163...2.047692843365260.999569257459424
173...2.061048420562940.999611881557209
183...2.045535679650040.999562003658341
193...1.953027616824180.999132591042693
203...2.085672091430470.999680898077519

Between Operator (_between)

# ...with the 'between' function
single_family[titanic["age"]._between(15, 24)]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
100%
123
x
Float(22)
100%
11...3.852273001022371.0
21...4.371976298820381.0
31...4.909954920578881.0
41...4.420746020260421.0
52...2.351375257163480.99997246355065
62...4.297285406218791.0
72...4.297285406218791.0
82...2.564949357461540.999997739670593
92...2.564949357461540.999997739670593
102...4.297285406218791.0
112...2.351375257163480.99997246355065
122...2.564949357461540.999997739670593
132...2.4849066497880.999993855787647
142...2.351375257163480.99997246355065
153...2.085672091430470.999680898077519
163...2.047692843365260.999569257459424
173...2.061048420562940.999611881557209
183...2.045535679650040.999562003658341
193...1.953027616824180.999132591042693
203...2.085672091430470.999680898077519

In Operator (_in)

# Identifies the passengers who came alone in 1st and 3rd class...

# ...with the 'in' method
single_family[titanic["pclass"]._in(1, 3)]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
99%
123
x
Float(22)
99%
11...3.902057514460061.0
21...3.569532696481371.0
31...3.433987204485150.999999999999966
41...3.852273001022371.0
51...3.455660314102120.999999999999983
61...4.371976298820381.0
71...-inf0.0
81...3.749504075930371.0
91...3.948595985658991.0
101...3.424125837416520.999999999999953
111...3.401197381662160.999999999999906
121...4.320705598542281.0
131...3.948595985658991.0
141...3.747148362237911.0
151...3.34990408727460.999999999999581
161...3.951243718581431.0
171...-inf0.0
181...4.909954920578881.0
191...3.921890164505811.0
201...3.424125837416520.999999999999953

Not In Operator (_not_in)

# ...with the 'not_in' method
single_family[titanic["pclass"]._not_in(2)]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
99%
123
x
Float(22)
99%
11...3.902057514460061.0
21...3.569532696481371.0
31...3.433987204485150.999999999999966
41...3.852273001022371.0
51...3.455660314102120.999999999999983
61...4.371976298820381.0
71...-inf0.0
81...3.749504075930371.0
91...3.948595985658991.0
101...3.424125837416520.999999999999953
111...3.401197381662160.999999999999906
121...4.320705598542281.0
131...3.948595985658991.0
141...3.747148362237911.0
151...3.34990408727460.999999999999581
161...3.951243718581431.0
171...-inf0.0
181...4.909954920578881.0
191...3.921890164505811.0
201...3.424125837416520.999999999999953

Or Operator (|)

# Or operator
single_family[(titanic["pclass"] == 1) | (titanic["pclass"] == 3)]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
99%
123
x
Float(22)
99%
11...3.902057514460061.0
21...3.569532696481371.0
31...3.433987204485150.999999999999966
41...3.852273001022371.0
51...3.455660314102120.999999999999983
61...4.371976298820381.0
71...-inf0.0
81...3.749504075930371.0
91...3.948595985658991.0
101...3.424125837416520.999999999999953
111...3.401197381662160.999999999999906
121...4.320705598542281.0
131...3.948595985658991.0
141...3.747148362237911.0
151...3.34990408727460.999999999999581
161...3.951243718581431.0
171...-inf0.0
181...4.909954920578881.0
191...3.921890164505811.0
201...3.424125837416520.999999999999953

Not Equal Operator (!=)

# ...with the not equal operator
single_family[titanic["pclass"] != 2]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
99%
123
x
Float(22)
99%
11...-inf0.0
21...4.320705598542281.0
31...3.417726683613370.999999999999943
41...3.921890164505811.0
51...3.678829118260431.0
61...1.60943791243410.993262053000915
71...3.951243718581431.0
81...3.242103950987410.999999999992282
91...3.650658241293741.0
101...3.912023005428151.0
111...3.357332568015220.999999999999661
121...3.391147045808650.999999999999874
131...3.81771232595691.0
141...3.258096538021480.999999999994891
151...3.279029747687950.999999999997052
161...3.912023005428151.0
171...3.511545438831020.999999999999997
181...3.279029747687950.999999999997052
191...5.353456381663081.0
201...3.279029747687950.999999999997052

‘Pythonic’ Invokation of Vertica Functions

You can easily apply Vertica functions to your vDataFrame. Here, we use Vertica’s COALESCE function to impute the age of the passengers in our dataset.

titanic["age"].count()
res = titanic["age"].count()
titanic["age"] = fun.coalesce(titanic["age"], titanic["age"].avg());

titanic["age"].count()
Out[3]: 1234.0

Slicing the vDataFrame

You can now slice the vDataFrame with indexing operators.

titanic[0:30]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
100%
123
x
Float(22)
100%
11...3.902057514460061.0
21...3.569532696481371.0
31...3.433987204485150.999999999999966
41...3.852273001022371.0
51...4.918367946844541.0
61...4.420746020260421.0
71...3.951243718581431.0
81...4.667440511714221.0
91...3.455660314102120.999999999999983
101...3.972176928247891.0
111...4.371976298820381.0
121...4.424547185829721.0
131...-inf0.0
141...3.749504075930371.0
151...3.948595985658991.0
161...3.951243718581431.0
171...3.424125837416520.999999999999953
181...3.401197381662160.999999999999906
191...4.320705598542281.0
201...3.948595985658991.0

To access a single row, we just have to specify the index.

titanic[0:1]
123
pclass
Integer
100%
...
123
ln_fare
Float(22)
100%
123
x
Float(22)
100%
11...3.902057514460061.0