~ubuntu-branches/debian/sid/python-feather-format/sid

« back to all changes in this revision

Viewing changes to feather/api.py

  • Committer: Package Import Robot
  • Author(s): ChangZhuo Chen (陳昌倬)
  • Date: 2016-03-30 19:01:30 UTC
  • Revision ID: package-import@ubuntu.com-20160330190130-b7o770i7o2drepi1
Tags: upstream-0.1.0
Import upstream version 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2016 Feather Developers
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
# http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
 
14
 
 
15
import six
 
16
 
 
17
import pandas as pd
 
18
 
 
19
import feather.ext as ext
 
20
 
 
21
 
 
22
def write_dataframe(df, path):
 
23
    '''
 
24
    Write a pandas.DataFrame to Feather format
 
25
    '''
 
26
    writer = ext.FeatherWriter(path)
 
27
 
 
28
    # TODO(wesm): pipeline conversion to Arrow memory layout
 
29
    for i, name in enumerate(df.columns):
 
30
        col = df.iloc[:, i]
 
31
 
 
32
        if not isinstance(name, six.string_types):
 
33
            name = str(name)
 
34
 
 
35
        writer.write_array(name, col)
 
36
 
 
37
    writer.close()
 
38
 
 
39
 
 
40
def read_dataframe(path, columns=None):
 
41
    """
 
42
    Read a pandas.DataFrame from Feather format
 
43
 
 
44
    Returns
 
45
    -------
 
46
    df : pandas.DataFrame
 
47
    """
 
48
    reader = ext.FeatherReader(path)
 
49
 
 
50
    # TODO(wesm): pipeline conversion to Arrow memory layout
 
51
    data = {}
 
52
    for i in range(reader.num_columns):
 
53
        name, arr = reader.read_array(i)
 
54
        data[name] = arr
 
55
 
 
56
    # TODO(wesm):
 
57
    return pd.DataFrame(data)