-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |