[Vaex 1.0.0-beta.6] how to split data from str to list.

2020. 8. 29. 13:48분석 Python/Vaex

728x90
import vaex

 

vaex에서는 List로 된 Object를 hdf5로 저장할 수 없어서 구분자 `[SEP]` 를 만들어서 string으로 만들었다.

그리고 다시 그것을 pandas처럼 split을 사용하여 처리하려고 하니 다음과 같은 에러가 발생했다.

 

data_vaex.feature.str.split("[SEP]")

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    400                         if cls is not object \
    401                                 and callable(cls.__dict__.get('__repr__')):
--> 402                             return _repr_pprint(obj, self, cycle)
    403 
    404             return _default_pprint(obj, self, cycle)

~/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    695     """A pprint that just redirects to the normal repr function."""
    696     # Find newlines and replace them with p.break_()
--> 697     output = repr(obj)
    698     for idx,output_line in enumerate(output.splitlines()):
    699         if idx:

~/anaconda3/lib/python3.6/site-packages/vaex/expression.py in __repr__(self)
    432 
    433     def __repr__(self):
--> 434         return self._repr_plain_()
    435 
    436     def _repr_plain_(self):

~/anaconda3/lib/python3.6/site-packages/vaex/expression.py in _repr_plain_(self)
    463         info = 'Expression = ' + expression + '\n'
    464         str_type = str
--> 465         dtype = self.dtype
    466         dtype = (str(dtype) if dtype != str_type else 'str')
    467         if self.expression in self.ds.get_column_names(hidden=True):

~/anaconda3/lib/python3.6/site-packages/vaex/expression.py in dtype(self)
    252     @property
    253     def dtype(self):
--> 254         return self.ds.dtype(self.expression)
    255 
    256     @property

~/anaconda3/lib/python3.6/site-packages/vaex/dataframe.py in dtype(self, expression, internal)
   2032             except:
   2033                 data = self.evaluate(expression, 0, 1, filtered=True, internal=True, parallel=False)
-> 2034             dtype = data.dtype
   2035         if not internal:
   2036             if dtype != str_type:

AttributeError: 'vaex.superstrings.StringListList' object has no attribute 'dtype'

아래 문서에서는 마치 split을 제공하는 것처럼 되어 있었는데..ㅠ

 

https://vaex.readthedocs.io/en/latest/api.html?highlight=string#string-operations

 

API documentation for vaex library — vaex 3.0.0 documentation

Parameters: x – expression or list of expressions, e.g. df.x, ‘x’, or [‘x, ‘y’] y – expression or list of expressions, e.g. df.x, ‘x’, or [‘x, ‘y’] limits – description for the min and max values for the expressions, e.g. ‘minma

vaex.readthedocs.io

그래서 처음에는 pandas_to_series로 변환해서 처리를 했다가, 그러면 별로 안 좋은 방법인 것 같아 찾아보니 아래와 같은 apply를 쓰는 방법이 있었다.

 

기존 방법

def split_by_sep(x:pd.Series) :
    return x.apply(lambda x x.split("[SEP]"))
data_vaex[LIST_TYPE_COL].to_pandas_df().apply(lambda x : split_by_sep(x),axis=1)

 

 

https://github.com/vaexio/vaex/issues/442

 

How to use apply on the dataframe · Issue #442 · vaexio/vaex

Hello. I have a question about custom function here, as apply method is not documented. import vaex df = vaex.example() df.apply?? I saw the example from #265 that apply can be used on a (virtual) ...

github.com

새로 찾은 방법

def split_by_sep(x:pd.Series) :
    return x.split("[SEP]")
    
data_vaex.apply(split_by_sep, [data_vaex.feature])

vaex로 바로 변환된 것을 알 수 있다!

 

아직 vaex가 아쉬운 부분이 많지만, 빅데이터 처리 시 좋은 점이 아주 많기 때문에 계속 업데이트가 되고 있기 때문에, 연습해둘 가치가 있는 패키지라고 생각된다.

728x90