diff --git a/.gitignore b/.gitignore index 894a44c..079f900 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ venv.bak/ # mypy .mypy_cache/ + +# pycharm +.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4694072 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: python +python: 3.6.7 +env: +- BOTO_CONFIG=/dev/null +install: +- pip install --upgrade pip +- pip install coverage +script: +- coverage run --include=kuma setup.py test +after_success: +- coveralls diff --git a/kuma/__init__.py b/kuma/__init__.py new file mode 100644 index 0000000..b2067f0 --- /dev/null +++ b/kuma/__init__.py @@ -0,0 +1,2 @@ +from kuma.kuma import SeriesAccessor +from kuma.kuma_pandas import kuma_pandas as pandas diff --git a/kuma/kuma.py b/kuma/kuma.py new file mode 100644 index 0000000..d31f203 --- /dev/null +++ b/kuma/kuma.py @@ -0,0 +1,10 @@ +import pandas as pd + + +@pd.api.extensions.register_series_accessor('kuma') +class SeriesAccessor(object): + def __init__(self, pandas_obj: pd.Series) -> None: + self._obj = pandas_obj + + def apply(self, func, **kwargs): + return self._obj.apply(func=func, **kwargs) if not self._obj.empty else [] diff --git a/kuma/kuma_pandas.py b/kuma/kuma_pandas.py new file mode 100644 index 0000000..6ab54ce --- /dev/null +++ b/kuma/kuma_pandas.py @@ -0,0 +1,13 @@ +import pandas as pd + + +class KumaSeries(object): + """To make IDE intellisense work well, functions are defined as staticmethod and named with `kuma_` prefix. + """ + @staticmethod + def kuma_apply(data: pd.Series, func, **kwargs): + return data.apply(func=func, **kwargs) if not data.empty else [] + + +def kuma_pandas(): + pd.Series.kuma_apply = KumaSeries.kuma_apply diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..270dd90 --- /dev/null +++ b/setup.py @@ -0,0 +1,28 @@ +from setuptools import setup, find_packages +from codecs import open +from os import path + +with open(path.join(path.abspath(path.dirname(__file__)), 'README.md'), encoding='utf-8') as f: + long_description = f.read() + + +install_requires = [ + 'pandas', +] + +setup( + name='kuma', + use_scm_version=True, + setup_requires=['setuptools_scm'], + description='A package which adds new functions to a pandas dataframe or series.', + long_description=long_description, + long_description_content_type="text/markdown", + author='M3, inc.', + url='https://github.com/m3dev/kuma', + license='MIT License', + packages=find_packages(), + install_requires=install_requires, + tests_require=[], + test_suite='test', + classifiers=['Programming Language :: Python :: 3.6'], +) diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_kuma_apply.py b/test/test_kuma_apply.py new file mode 100644 index 0000000..a99ef44 --- /dev/null +++ b/test/test_kuma_apply.py @@ -0,0 +1,20 @@ +import unittest + +import pandas as pd + +import kuma + +kuma.pandas() + + +class TestKumaApply(unittest.TestCase): + def test_with_empty(self): + df = pd.DataFrame(columns=['input']) + df['result'] = df['input'].kuma_apply(lambda x: x + 1) + self.assertEqual(len(df['result']), 0) + + def test_with_not_empty(self): + df = pd.DataFrame(dict(input=[0, 1])) + df['result'] = df['input'].kuma_apply(lambda x: x + 1) + self.assertEqual(len(df['result']), 2) + diff --git a/test/test_series_accessor.py b/test/test_series_accessor.py new file mode 100644 index 0000000..bbcb385 --- /dev/null +++ b/test/test_series_accessor.py @@ -0,0 +1,15 @@ +import unittest +import pandas as pd +import kuma + + +class TestSeriesAccessor(unittest.TestCase): + def test_apply_with(self): + df = pd.DataFrame(dict(input=[0, 1])) + df['result'] = df['input'].kuma.apply(lambda x: x + 1) + self.assertListEqual(df['result'].tolist(), [1, 2]) + + def test_apply_with_empty_df(self): + df = pd.DataFrame(columns=['input']) + df['result'] = df['input'].kuma.apply(lambda x: x + 1) + self.assertEqual(len(df['result']), 0) diff --git a/yapf.ini b/yapf.ini new file mode 100644 index 0000000..c9a88d5 --- /dev/null +++ b/yapf.ini @@ -0,0 +1,3 @@ +[style] +based_on_style = pep8 +column_limit = 120