Skip to content
This repository was archived by the owner on Jun 26, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# pycharm
.idea
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions kuma/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from kuma.kuma import SeriesAccessor
from kuma.kuma_pandas import kuma_pandas as pandas
10 changes: 10 additions & 0 deletions kuma/kuma.py
Original file line number Diff line number Diff line change
@@ -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 []
13 changes: 13 additions & 0 deletions kuma/kuma_pandas.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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'],
)
Empty file added test/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions test/test_kuma_apply.py
Original file line number Diff line number Diff line change
@@ -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)

15 changes: 15 additions & 0 deletions test/test_series_accessor.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions yapf.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[style]
based_on_style = pep8
column_limit = 120