vaex icon indicating copy to clipboard operation
vaex copied to clipboard

How to use DataFrame.replace in Vaex

Open fansichao opened this issue 6 years ago • 16 comments

Version:

  • Python3.6
  • Vaex 1.0.0-beta.6
  • Pandas 0.25.3

I want to use Vaex replace Pandas But some func I don't find in Vaex. exp: Pandas.DataFrame.replace

Is there a comparison table with pandas and vaex func ?

Thanks

fansichao avatar Dec 12 '19 08:12 fansichao

Hi,

  • Vaex 1.0.0-beta.6 Where did you get this version from, it is very old, could you check with pip show vaex what the version number is?

This function does not exist yet, and there is no comparison table. When it makes sense we keep the name the same, so if it is missing, it is probably not supported. If we deviate from Pandas, we usually say so in the docstring.

Would you be happy if we added support for replacing a single numerical value with a different numerical value, and only for a single expression, e.g.:

df['x'] = df.x.replace(42, 24)

For strings we have a replace: https://docs.vaex.io/en/latest/api.html#vaex.expression.StringOperations.replace

cheers,

Maarten

maartenbreddels avatar Dec 12 '19 08:12 maartenbreddels

(env36) [scfan@fdm ~]$ pip show vaex
Name: vaex
Version: 2.4.0
Summary: Out-of-Core DataFrames to visualize and explore big tabular datasets
Home-page: https://www.github.com/maartenbreddels/vaex
Author: Maarten A. Breddels
Author-email: [email protected]
License: MIT
Location: /home/scfan/env36/lib/python3.6/site-packages
Requires: vaex-ml, vaex-astro, vaex-arrow, vaex-jupyter, vaex-viz, vaex-hdf5, vaex-core, vaex-server
Required-by: 

But I use python

(env36) [scfan@fdm ~]$ python
Python 3.6.8 (default, Aug  7 2019, 17:28:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import vaex
>>> vaex.__version__
'1.0.0-beta.6'
>>> 

different way show different version

fansichao avatar Dec 12 '19 08:12 fansichao

that's a leftover of an old version of vaex, we should fix that.

maartenbreddels avatar Dec 12 '19 08:12 maartenbreddels

OK. Thanks for your answer.

fansichao avatar Dec 12 '19 08:12 fansichao

But we didn't solve your problem yet :) You can also try using df.x.map(..) https://docs.vaex.io/en/latest/api.html#vaex.expression.Expression.map

e.g.

df['x'] = df.x.map({42: 24})

maartenbreddels avatar Dec 12 '19 08:12 maartenbreddels

e.g. Pandas:

  • data = data.replace(to_replace='nan',value='')

How to replace in a good way?

In [51]: df.col_bd214ea32724e6ac78b8574588e7b637[0:2]                                                                                                              
Out[51]: 
Expression = col_bd214ea32724e6ac78b8574588e7b637
Length: 2 dtype: str (column)
-----------------------------
0
1

In [52]: df.col_bd214ea32724e6ac78b8574588e7b637.replace(0,111)                                                                                                    
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-52-8259c66fe373> in <module>
----> 1 df.col_bd214ea32724e6ac78b8574588e7b637.replace(0,111)

AttributeError: 'Expression' object has no attribute 'replace'

In [53]: type(df)                                                                                                                                                  
Out[53]: vaex.dataframe.DataFrameArrays

# use Map . Do I need input all value in this mapper ?
ValueError: Missing 201 values in mapper

Wanted to know if it's good practice to do that and what would be the best way to do that?

fansichao avatar Dec 12 '19 09:12 fansichao

Hey @fansichao

A possible solution to your problem is to use where:

Consider this example:

df = vaex.from_arrays(x=np.array([0, 1, 2, 3, 4, 0, 0, 5]))
# Syntac of where: (condition, fill value, column to fill)
df['x_new'] = df.func.where(df.x == 0, 10, df.x)
print(df)

You can also overwrite 'x' if you wish, but since new virtual columns in vaex take no memory, I often keep everything.. just because I can (and I can debug my pipelines easily).

The .map method is also quite powerful, but maybe use in a bit difference circumstances: when you want to map all/most of the values in a column to other values (like mapping give some distinct codes meaning for example). .map by default will try to map everything (you need to provide dictionary with key-value pairs). If you check the API docs, you will see that there are options of what to do in case a value is encountered in the column that is not specifying in the mapping dictionary. All this is just FYI, I guess the .where function is applicable for your problem.

Hope this help. J.

JovanVeljanoski avatar Dec 13 '19 14:12 JovanVeljanoski

in case you one needs to map only some certain values of a column to a value and leave the rest as is, the following does the trick:

df['x_new'] = df.func.where(df.x.isin(to_replace_list), 10, df.x)

aanilpala avatar Dec 04 '20 14:12 aanilpala

There is also df.x.map(mapping_dict) that can sometimes be quite handy.

JovanVeljanoski avatar Dec 04 '20 14:12 JovanVeljanoski

@JovanVeljanoski does it do "leave the rest as is" part?

aanilpala avatar Dec 04 '20 14:12 aanilpala

Ah you are right. If something is not defined in the mapping dict, the best it can do it assign a default value of your choosing.

JovanVeljanoski avatar Dec 04 '20 17:12 JovanVeljanoski

df.func.where doesn't work for this purpose in 4.0.0a16a. Instead I used .apply to obtain the same results.

df["x_new"] = df["x"].apply(lambda val: 10 if val in to_replace_list else val)

aanilpala avatar Jan 27 '21 12:01 aanilpala

doesn't work

What does this mean, does it give an error? If so, which error do you get?

maartenbreddels avatar Jan 27 '21 13:01 maartenbreddels

I've just reproduced it now. Turns out that, it works normally fine but when the value to replace is np.nan, it causes the following error

Error evaluating: RuntimeError('Oops, requesting column nan from dataset, but it does not exist')

df
  #    a  b           c        d  e          num_a
  0  1    '2.8'     1        1    '1.0'        1
  1  2.9  '0.6'   nan    -9998    '-9998'      2.9
  2  2.5  '-0.7'    2.5      2.5  '2.5'        2.5
  3  6.8  '5'       4        4    '4.0'        6.8
(Pdb) df.func.where(df.a.isin((-9998, -9998.0, -9999, -9999.0)), 10, df.a)
Expression = where(isin_set(a, var_isin_ordered_set), 10, a)
Length: 4 dtype: float64 (expression)
-------------------------------------
0    1
1  2.9
2  2.5
3  6.8
(Pdb) df.func.where(df.a.isin((-9998, -9998.0, -9999, -9999.0)), np.nan, df.a)
Expression = where(isin_set(a, var_isin_ordered_set_1), nan, a)
Length: 4 dtype: float64 (expression)
-------------------------------------
Error evaluating: RuntimeError('Oops, requesting column nan from dataset, but it does not exist')

aanilpala avatar Jan 27 '21 13:01 aanilpala

Hmm, could you open a new issue with a reproducible example, I think you have found a bug!

maartenbreddels avatar Jan 27 '21 18:01 maartenbreddels

here it is: https://github.com/vaexio/vaex/issues/1199

aanilpala avatar Jan 28 '21 14:01 aanilpala