Skip to content

Commit

Permalink
add pandas series
Browse files Browse the repository at this point in the history
  • Loading branch information
acezen committed Jan 4, 2021
1 parent d87643c commit 71e3acf
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/vineyard/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from vineyard.data.arrow import register_arrow_types
from vineyard.data.tensor import register_tensor_types
from vineyard.data.index import register_index_types
from vineyard.data.series import register_series_types
from vineyard.data.dataframe import register_dataframe_types
from vineyard.data.graph import register_graph_types

Expand All @@ -33,6 +34,7 @@ def register_builtin_types(builder_ctx, resolver_ctx):
register_arrow_types(builder_ctx, resolver_ctx)
register_tensor_types(builder_ctx, resolver_ctx)
register_index_types(builder_ctx, resolver_ctx)
register_series_types(builder_ctx, resolver_ctx)
register_dataframe_types(builder_ctx, resolver_ctx)
register_graph_types(builder_ctx, resolver_ctx)

Expand Down
51 changes: 51 additions & 0 deletions python/vineyard/data/series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2020 Alibaba Group Holding Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import json
import numpy as np
import pandas as pd
from pandas.core.internals.blocks import Block
from pandas.core.internals.managers import SingleBlockManager

from vineyard._C import ObjectMeta


def pandas_series_builder(client, value, builder, **kw):
meta = ObjectMeta()
meta['typename'] = 'vineyard::Series'
meta['name'] = json.dumps(value.name)
meta.add_member('index_', builder.run(client, value.index))
meta.add_member('value_', builder.run(client, value.to_numpy(), **kw))
return client.create_metadata(meta)


def pandas_series_resolver(obj, resolver):
meta = obj.meta
name = json.loads(meta['name'])
index = resolver.run(obj.member('index_'))
np_value = resolver.run(obj.member('value_'))
block = Block(np_value, slice(0, len(np_value), 1), ndim=1)
return pd.Series(SingleBlockManager(block, index), name=name)


def register_series_types(builder_ctx, resolver_ctx):
if builder_ctx is not None:
builder_ctx.register(pd.Series, pandas_series_builder)

if resolver_ctx is not None:
resolver_ctx.register('vineyard::Series', pandas_series_resolver)
33 changes: 33 additions & 0 deletions python/vineyard/data/tests/test_series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2020 Alibaba Group Holding Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import pandas as pd
import pytest
import numpy as np

import vineyard
from vineyard.core import default_builder_context, default_resolver_context
from vineyard.data import register_builtin_types

register_builtin_types(default_builder_context, default_resolver_context)


def test_pandas_series(vineyard_client):
s = pd.Series([1, 3, 5, np.nan, 6, 8], name='foo')
object_id = vineyard_client.put(s)
pd.testing.assert_series_equal(s, vineyard_client.get(object_id))

0 comments on commit 71e3acf

Please sign in to comment.