github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/dataframe/frame_base_test.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  import unittest
    18  
    19  import pandas as pd
    20  
    21  from apache_beam.dataframe import expressions
    22  from apache_beam.dataframe import frame_base
    23  from apache_beam.dataframe import frames
    24  
    25  
    26  class FrameBaseTest(unittest.TestCase):
    27    def test_elementwise_func(self):
    28      a = pd.Series([1, 2, 3])
    29      b = pd.Series([100, 200, 300])
    30      empty_proxy = a[:0]
    31      x = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
    32      y = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
    33      sub = frame_base._elementwise_function(lambda x, y: x - y)
    34  
    35      session = expressions.Session({x._expr: a, y._expr: b})
    36      self.assertTrue(sub(x, y)._expr.evaluate_at(session).equals(a - b))
    37      self.assertTrue(sub(x, 1)._expr.evaluate_at(session).equals(a - 1))
    38      self.assertTrue(sub(1, x)._expr.evaluate_at(session).equals(1 - a))
    39      self.assertTrue(sub(x, b)._expr.evaluate_at(session).equals(a - b))
    40      self.assertTrue(sub(a, y)._expr.evaluate_at(session).equals(a - b))
    41  
    42    def test_elementwise_func_kwarg(self):
    43      a = pd.Series([1, 2, 3])
    44      b = pd.Series([100, 200, 300])
    45      empty_proxy = a[:0]
    46      x = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
    47      y = frames.DeferredSeries(expressions.PlaceholderExpression(empty_proxy))
    48      sub = frame_base._elementwise_function(lambda x, y=1: x - y)
    49  
    50      session = expressions.Session({x._expr: a, y._expr: b})
    51      self.assertTrue(sub(x, y=y)._expr.evaluate_at(session).equals(a - b))
    52      self.assertTrue(sub(x)._expr.evaluate_at(session).equals(a - 1))
    53      self.assertTrue(sub(1, y=x)._expr.evaluate_at(session).equals(1 - a))
    54      self.assertTrue(sub(x, y=b)._expr.evaluate_at(session).equals(a - b))
    55      self.assertTrue(sub(a, y=y)._expr.evaluate_at(session).equals(a - b))
    56      self.assertTrue(sub(x, y)._expr.evaluate_at(session).equals(a - b))
    57  
    58    def test_maybe_inplace(self):
    59      @frame_base.maybe_inplace
    60      def add_one(frame):
    61        return frame + 1
    62  
    63      frames.DeferredSeries.add_one = add_one
    64      original_expr = expressions.PlaceholderExpression(pd.Series([1, 2, 3]))
    65      x = frames.DeferredSeries(original_expr)
    66      x.add_one()
    67      self.assertIs(x._expr, original_expr)
    68      x.add_one(inplace=False)
    69      self.assertIs(x._expr, original_expr)
    70      x.add_one(inplace=True)
    71      self.assertIsNot(x._expr, original_expr)
    72  
    73    def test_args_to_kwargs(self):
    74      class Base(object):
    75        def func(self, a=1, b=2, c=3):
    76          pass
    77  
    78      class Proxy(object):
    79        @frame_base.args_to_kwargs(Base)
    80        def func(self, **kwargs):
    81          return kwargs
    82  
    83      proxy = Proxy()
    84      # pylint: disable=too-many-function-args
    85      self.assertEqual(proxy.func(), {})
    86      self.assertEqual(proxy.func(100), {'a': 100})
    87      self.assertEqual(proxy.func(2, 4, 6), {'a': 2, 'b': 4, 'c': 6})
    88      self.assertEqual(proxy.func(2, c=6), {'a': 2, 'c': 6})
    89      self.assertEqual(proxy.func(c=6, a=2), {'a': 2, 'c': 6})
    90  
    91    def test_args_to_kwargs_populates_defaults(self):
    92      class Base(object):
    93        def func(self, a=1, b=2, c=3):
    94          pass
    95  
    96      class Proxy(object):
    97        @frame_base.args_to_kwargs(Base)
    98        @frame_base.populate_defaults(Base)
    99        def func(self, a, c=1000, **kwargs):
   100          return dict(kwargs, a=a, c=c)
   101  
   102      proxy = Proxy()
   103      # pylint: disable=too-many-function-args
   104      self.assertEqual(proxy.func(), {'a': 1, 'c': 1000})
   105      self.assertEqual(proxy.func(100), {'a': 100, 'c': 1000})
   106      self.assertEqual(proxy.func(2, 4, 6), {'a': 2, 'b': 4, 'c': 6})
   107      self.assertEqual(proxy.func(2, c=6), {'a': 2, 'c': 6})
   108      self.assertEqual(proxy.func(c=6, a=2), {'a': 2, 'c': 6})
   109      self.assertEqual(proxy.func(c=6), {'a': 1, 'c': 6})
   110  
   111  
   112  if __name__ == '__main__':
   113    unittest.main()