@@ -0,0 +1,3263 @@
+import pandas as pd%0Afrom multiprocessing import cpu_count%0Afrom dask import dataframe as dd%0Afrom dask.multiprocessing import get%0Afrom numba import jit%0Aimport timeit%0A%0A%0Adef pd_apply(df, myfunc, *args, **kwargs):%0A    def wrapped():%0A        df.apply(myfunc, args=args, **kwargs)%0A    return wrapped%0A%0A%0Adef dask_apply(df, npartitions, myfunc, *args, **kwargs):%0A    if type(df) == pd.DataFrame:%0A        kwargs.pop('meta')%0A        tmp = df.iloc%5B:1,:%5D.apply(myfunc, args=args, **kwargs)%0A        meta = %7Bc: tmp%5Bc%5D.dtype for c in tmp.columns%7D%0A        return dd.from_pandas(df, npartitions=npartitions).apply(myfunc, *args, axis=1, **kwargs, meta=meta).compute(get=get)%0A    else:%0A        meta = kwargs.pop('meta')%0A        return dd.from_pandas(df, npartitions=npartitions).map_partitions(myfunc, *args, **kwargs, meta=meta).compute(get=get)%0A%0A    %0Adef swiftapply(df, myfunc, *args, **kwargs):%0A    %22%22%22%0A    Efficiently apply any function to a pandas dataframe or series%0A    in the fastest available manner%0A    :param df: The dataframe or series to apply the function to%0A    :param myfunc: The function you wish to apply%0A    :param args: The positional arguments of the function%0A    :param kwargs: The key word arguments of the function%0A        You can also specify npartitions and dask_threshold%0A        npartitions will affect the speed of dask multiprocessing%0A        dask_threshold is the maximum allowed time (in seconds) for a normal pandas apply%0A            before switching to a dask operation%0A    :return: The new dataframe/series with the function applied as quickly as possible%0A    %22%22%22%0A    if 'npartitions' in kwargs.keys():%0A        npartitions = kwargs.pop('npartitions')%0A    else:%0A        npartitions = cpu_count() * 2%0A    if 'dask_threshold' in kwargs.keys():%0A        dask_threshold = kwargs.pop('dask_threshold')%0A    else:%0A        dask_threshold = 1%0A    %0A    if myfunc is not str:%0A        try:%0A            mynumbafunc = jit(myfunc)%0A            return mynumbafunc(df, *args, **kwargs)%0A        except:%0A            try:%0A                return myfunc(df, *args, **kwargs)%0A            except: %0A                try:%0A                    samp = df.sample(n=1000)%0A                except:%0A                    samp = df.sample(frac=0.1)%0A%0A                wrapped = pd_apply(samp, myfunc, *args, **kwargs)%0A                n_repeats = 3%0A%0A                timed = timeit.timeit(wrapped, number=n_repeats)%0A                samp_proc_est = timed/n_repeats%0A                est_apply_duration = samp_proc_est / len(samp) * df.shape%5B0%5D%0A%0A%0A                kwargs%5B'meta'%5D = myfunc(df.iloc%5B0%5D, *args, **kwargs)%0A                if (est_apply_duration %3E dask_threshold): %0A                    try:%0A                        mynumbafunc = jit(myfunc)%0A                        return dask_apply(df, npartitions, mynumbafunc, *args, **kwargs)%0A                    except:%0A                        return dask_apply(df, npartitions, myfunc, *args, **kwargs)%0A                else:%0A                    kwargs.pop('meta')%0A                    try:%0A                        mynumbafunc = jit(myfunc)%0A                        return df.apply(mynumbafunc, args=args, **kwargs)%0A                    except:%0A                        return df.apply(myfunc, args=args, **kwargs)%0A    else:%0A        return df.astype(str)

Refactor for pypi