-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathframe_base_test.py
More file actions
194 lines (160 loc) · 7.25 KB
/
frame_base_test.py
File metadata and controls
194 lines (160 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import pandas as pd
from apache_beam.dataframe import expressions
from apache_beam.dataframe import frame_base
from apache_beam.dataframe import frames
class FrameBaseTest(unittest.TestCase):
def test_elementwise_func(self):
a = pd.Series([1, 2, 3])
b = pd.Series([100, 200, 300])
empty_proxy = a[:0]
x = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
y = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
sub = frame_base._elementwise_function(lambda x, y: x - y)
session = expressions.Session({x._expr: a, y._expr: b})
self.assertTrue(sub(x, y)._expr.evaluate_at(session).equals(a - b))
self.assertTrue(sub(x, 1)._expr.evaluate_at(session).equals(a - 1))
self.assertTrue(sub(1, x)._expr.evaluate_at(session).equals(1 - a))
self.assertTrue(sub(x, b)._expr.evaluate_at(session).equals(a - b))
self.assertTrue(sub(a, y)._expr.evaluate_at(session).equals(a - b))
def test_elementwise_func_kwarg(self):
a = pd.Series([1, 2, 3])
b = pd.Series([100, 200, 300])
empty_proxy = a[:0]
x = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
y = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
sub = frame_base._elementwise_function(lambda x, y=1: x - y)
session = expressions.Session({x._expr: a, y._expr: b})
self.assertTrue(sub(x, y=y)._expr.evaluate_at(session).equals(a - b))
self.assertTrue(sub(x)._expr.evaluate_at(session).equals(a - 1))
self.assertTrue(sub(1, y=x)._expr.evaluate_at(session).equals(1 - a))
self.assertTrue(sub(x, y=b)._expr.evaluate_at(session).equals(a - b))
self.assertTrue(sub(a, y=y)._expr.evaluate_at(session).equals(a - b))
self.assertTrue(sub(x, y)._expr.evaluate_at(session).equals(a - b))
def test_maybe_inplace(self):
@frame_base.maybe_inplace
def add_one(frame):
return frame + 1
frames.DeferredSeries.add_one = add_one
original_expr = expressions.PlaceholderExpression(pd.Series([1, 2, 3]))
x = frames.DeferredSeries(original_expr)
x.add_one()
self.assertIs(x._expr, original_expr)
x.add_one(inplace=False)
self.assertIs(x._expr, original_expr)
x.add_one(inplace=True)
self.assertIsNot(x._expr, original_expr)
def test_args_to_kwargs(self):
class Base(object):
def func(self, a=1, b=2, c=3, *, kw_only=4):
pass
class Proxy(object):
@frame_base.args_to_kwargs(Base)
def func(self, **kwargs):
return kwargs
proxy = Proxy()
# pylint: disable=too-many-function-args
self.assertEqual(proxy.func(), {})
self.assertEqual(proxy.func(100), {'a': 100})
self.assertEqual(proxy.func(2, 4, 6), {'a': 2, 'b': 4, 'c': 6})
self.assertEqual(proxy.func(2, c=6), {'a': 2, 'c': 6})
self.assertEqual(proxy.func(c=6, a=2), {'a': 2, 'c': 6})
self.assertEqual(proxy.func(2, kw_only=20), {'a': 2, 'kw_only': 20})
with self.assertRaises(TypeError): # got too many positioned arguments
proxy.func(2, 4, 6, 8)
def test_args_to_kwargs_populates_defaults(self):
class Base(object):
def func(self, a=1, b=2, c=3):
pass
def func_removed_args(self, a):
pass
class Proxy(object):
@frame_base.args_to_kwargs(Base)
@frame_base.populate_defaults(Base)
def func(self, a, c=1000, **kwargs):
return dict(kwargs, a=a, c=c)
@frame_base.args_to_kwargs(Base, removed_method=True)
@frame_base.populate_defaults(Base, removed_method=True)
def func_removed_method(self, a, **kwargs):
return dict(kwargs, a=a)
@frame_base.args_to_kwargs(Base, removed_args=['c'])
@frame_base.populate_defaults(Base, removed_args=['c'])
def func_removed_args(self, a, c, **kwargs):
return dict(kwargs, a=a)
proxy = Proxy()
# pylint: disable=too-many-function-args,no-value-for-parameter
self.assertEqual(proxy.func(), {'a': 1, 'c': 1000})
self.assertEqual(proxy.func(100), {'a': 100, 'c': 1000})
self.assertEqual(proxy.func(2, 4, 6), {'a': 2, 'b': 4, 'c': 6})
self.assertEqual(proxy.func(2, c=6), {'a': 2, 'c': 6})
self.assertEqual(proxy.func(c=6, a=2), {'a': 2, 'c': 6})
self.assertEqual(proxy.func(c=6), {'a': 1, 'c': 6})
with self.assertRaises(TypeError): # missing 1 required positional argument
proxy.func_removed_method()
self.assertEqual(proxy.func_removed_method(12, c=100), {'a': 12, 'c': 100})
with self.assertRaises(TypeError): # missing 1 required positional argument
proxy.func_removed_args()
self.assertEqual(proxy.func_removed_args(12, d=100), {'a': 12, 'd': 100})
def test_args_to_kwargs_populates_default_handles_kw_only(self):
class Base(object):
def func(self, a, b=2, c=3, *, kw_only=4):
pass
class ProxyUsesKwOnly(object):
@frame_base.args_to_kwargs(Base)
@frame_base.populate_defaults(Base)
def func(self, a, kw_only, **kwargs):
return dict(kwargs, a=a, kw_only=kw_only)
proxy = ProxyUsesKwOnly()
# pylint: disable=too-many-function-args,no-value-for-parameter
with self.assertRaises(TypeError): # missing 1 required positional argument
proxy.func()
self.assertEqual(proxy.func(100), {'a': 100, 'kw_only': 4})
self.assertEqual(
proxy.func(2, 4, 6, kw_only=8), {
'a': 2, 'b': 4, 'c': 6, 'kw_only': 8
})
with self.assertRaises(TypeError):
proxy.func(2, 4, 6, 8) # got too many positioned arguments
class ProxyDoesntUseKwOnly(object):
@frame_base.args_to_kwargs(Base)
@frame_base.populate_defaults(Base)
def func(self, a, **kwargs):
return dict(kwargs, a=a)
proxy = ProxyDoesntUseKwOnly()
# pylint: disable=too-many-function-args,no-value-for-parameter
with self.assertRaises(TypeError): # missing 1 required positional argument
proxy.func()
self.assertEqual(proxy.func(100), {'a': 100})
self.assertEqual(
proxy.func(2, 4, 6, kw_only=8), {
'a': 2, 'b': 4, 'c': 6, 'kw_only': 8
})
def test_populate_defaults_overwrites_copy(self):
class Base(object):
def func(self, a=1, b=2, c=3, *, copy=None):
pass
class Proxy(object):
@frame_base.args_to_kwargs(Base)
@frame_base.populate_defaults(Base)
def func(self, a, copy, **kwargs):
return dict(kwargs, a=a, copy=copy)
proxy = Proxy()
self.assertEqual(proxy.func(), {'a': 1, 'copy': True})
self.assertEqual(proxy.func(copy=False), {'a': 1, 'copy': False})
if __name__ == '__main__':
unittest.main()