Невозможно прочитать SparseDataFrame из файла hdf - PullRequest
0 голосов
/ 05 октября 2018

Вот пример кода для создания csr_matrix, который я затем преобразовываю в SparseDataFrame и записываю в файл hdf5.

from scipy import sparse
from numpy import array
import pandas as pd
I = array([0,3,1,0])
J = array([0,3,1,2])
V = array([4,5,7,9])
A = sparse.coo_matrix((V,(I,J)),shape=(4,4))

Затем я записываю его в файл hdf5 следующим образом.

df = pd.SparseDataFrame(A)
df.to_hdf("/tmp/tmp.hdf", "my_data")

Теперь, если я пытаюсь прочитать его обратно, выдается исключение - «NotImplementedError: запуск и / или остановка не поддерживаются при фиксированном разреженном чтении».Это странно (похоже на ошибку), поскольку я могу писать в фиксированном разреженном формате, но не читаю его.

df2 = pd.read_hdf("/tmp/tmp.hdf", "my_data")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 394, in read_hdf
    return store.select(key, auto_close=auto_close, **kwargs)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 741, in select
    return it.get_result()
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 1483, in get_result
    results = self.func(self.start, self.stop, where)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 734, in func
    columns=columns)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 2855, in read
    kwargs = self.validate_read(kwargs)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 2821, in validate_read
    raise NotImplementedError("start and/or stop are not supported "
NotImplementedError: start and/or stop are not supported in fixed Sparse reading

Любые предложения о том, как это можно преодолеть?

У меня естьпопытался записать в формате таблицы, но здесь сама запись не удалась.

    df.to_hdf("/tmp/tmp.hdf", "my_data", format='table')
Traceback (most recent call last):
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 1313, in _create_storer
    return globals()[_TABLE_MAP[tt]](self, group, **kwargs)
KeyError: None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/core/generic.py", line 1996, in to_hdf
    return pytables.to_hdf(path_or_buf, key, self, **kwargs)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 279, in to_hdf
    f(store)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 273, in <lambda>
    f = lambda store: store.put(key, value, **kwargs)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 890, in put
    self._write_to_group(key, value, append=append, **kwargs)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 1349, in _write_to_group
    encoding=encoding, **kwargs)
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 1315, in _create_storer
    error('_TABLE_MAP')
  File "/Users/speaktribe/.virtualenvs/domain_classifier-g6h5ez5L/lib/python3.6/site-packages/pandas/io/pytables.py", line 1239, in error
    % (t, group, type(value), format, append, kwargs)
TypeError: cannot properly create the storer for: [_TABLE_MAP] [group->/my_data (Group) '',value-><class 'pandas.core.sparse.frame.SparseDataFrame'>,format->table,append->False,kwargs->{'encoding': None}]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...