从pandas DataFrame加载pyarrow木地板时保留索引

时间:2018-12-05 21:07:06

标签: python pandas dictionary dataframe pyarrow

我需要将具有dict值的dict转换为实木复合地板,我的数据如下所示:

{"KEY":{"2018-12-06":250.0,"2018-12-07":234.0}}

我要转换为pandas数据框,然后写入pyarrow表:

import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq

data = {"KEY":{"2018-12-06":250.0,"2018-12-07":234.0}}
df = pd.DataFrame.from_dict(data, orient='index')
table = pa.Table.from_pandas(df, preserve_index=False)
pq.write_table(table, 'file.parquet', flavor='spark')

我最终得到的数据只有日期和值,而没有字典的键。

{"2018-12-06":250.0,"2018-12-07":234.0}

我需要的还有数据的密钥:

{"KEY": {"2018-12-06":250.0,"2018-12-07":234.0}}

2 个答案:

答案 0 :(得分:3)

如果要保留索引,则应这样指定;设置preserve_index=True

table = pa.Table.from_pandas(df, preserve_index=True)

pq.write_table(table, 'file.parquet', flavor='spark')
pq.read_table('file.parquet').to_pandas()  # Index is preserved.

     2018-12-06  2018-12-07
KEY       250.0       234.0

答案 1 :(得分:0)

我观察到一个相关但独立的问题,其中 DateTimeIndex 的频率类型在从熊猫到表的往返行程中没有保留。

例如:

    >>> import pandas as pd
    >>> import pyarrow as pa
    >>> from collections import OrderedDict
    >>>
    >>>
    >>> pd.__version__
    '1.1.5'
    >>>
    >>> pa.__version__
    '4.0.1'
    >>>
    >>> dates = pd.date_range(start='2016-04-01', periods=4, name='DATE')
    >>> dict_data = OrderedDict()
    >>> dict_data['A'] = list('AABB')
    >>> dict_data['B'] = list('abab')
    >>> dict_data['C'] = list('wxyz')
    >>> dict_data['D'] = range(0, 4)
    >>> df = pd.DataFrame.from_dict(dict_data)
    >>> df = df.set_index(dates)
    >>>
    >>> df.index
    DatetimeIndex(['2016-04-01', '2016-04-02', '2016-04-03', '2016-04-04'], dtype='datetime64[ns]', name='DATE', freq='D')
    >>>
    >>> table = pa.Table.from_pandas(df, preserve_index=True)
    >>> df2 = table.to_pandas()
    >>> df2.index
    DatetimeIndex(['2016-04-01', '2016-04-02', '2016-04-03', '2016-04-04'], dtype='datetime64[ns]', name='DATE', freq=None)