# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

def my_func(x, y):
    pass


def my_sq(x):
    return x**2


my_sq(2)

my_sq(4)

#Testing the function
assert my_sq(4) == 16


def avg_2(x,y):
    return (x+y)/2


avg_2(10,1)

import pandas as pd

df = pd.DataFrame({
    'a': [10,20,30],
    'b': [20,30,40]
})

df

df['a'] ** 2

df['a'].apply(my_sq)


def my_exp(x, e):
    return x ** e


my_exp(2,10)

df['a'].apply(my_exp, e=4)


def pritn_me(x):
    print(x)


df.apply(pritn_me)


def avg(x, y, z):
    return (x + y + z)/3


df.apply(avg)

import numpy as np


def avg_apply(col):
    return np.mean(col)


df.apply(avg_apply)


def avg(col):
    x = col[0]
    y = col[1]
    z = col[2]
    return (x + y + z)/3


df.apply(avg)

df.apply(avg,axis=0)

    def avg_2_mod(x,y):
        if (x==20):
            return np.NAN
        else:
            return (x+y)/2

avg_2_mod(df['a'],df['b'])

avg_2_mod_vec=np.vectorize(avg_2_mod)

avg_2_mod_vec(df['a'],df['b'])


@np.vectorize
def avg_2_mod(x,y):
    if (x==20):
        return np.NAN
    else:
        return (x+y)/2


avg_2_mod(df['a'],df['b'])

import numba


@numba.vectorize
def avg_2_mod_numba(x,y):
    if (x==20):
        return np.NAN
    else:
        return (x+y)/2


avg_2_mod_numba(df['a'],df['b'])

avg_2_mod_numba(df['a'].values,df['b'].values)

# %%timeit
avg_2(df['a'],df['b'])

# %%timeit
avg_2_mod(df['a'],df['b'])

# %%timeit
avg_2_mod_numba(df['a'].values,df['b'].values)



StarcoderdataJupyterScriptsDedupFiltered