modin-project / modin

Modin: Scale your Pandas workflows by changing a single line of code
http://modin.readthedocs.io
Apache License 2.0
9.91k stars 653 forks source link

BUG: modin.pandas.pivot_table raises ValueError when given pandas dataframe argument #6243

Open zulee1711 opened 1 year ago

zulee1711 commented 1 year ago

Modin version checks

Reproducible Example

import pandas as pd
import modin.pandas as mpd
import pandas as pd
import modin.pandas as mpd
x = [{'date': pd.Timestamp('2020-01-03 00:00:00'),
  'ticker': 'CTG',
  7: 0.08},
 {'date': pd.Timestamp('2020-01-03 00:00:00'),
  'ticker': 'CTI',
  7: 0.02},
 {'date': pd.Timestamp('2020-01-03 00:00:00'),
  'ticker': 'DIG',
  7: 0.01},
 {'date': pd.Timestamp('2020-01-03 00:00:00'),
  'ticker': 'DRC',
  7: 0.069},
 {'date': pd.Timestamp('2020-01-03 00:00:00'),
  'ticker': 'HNG',
  7: 0.029},
{'date': pd.Timestamp('2020-01-04 00:00:00'),
  'ticker': 'CTG',
  7: 0.081},
 {'date': pd.Timestamp('2020-01-04 00:00:00'),
  'ticker': 'CTI',
  7: 0.025},
 {'date': pd.Timestamp('2020-01-04 00:00:00'),
  'ticker': 'DIG',
  7: 0.013},
 {'date': pd.Timestamp('2020-01-04 00:00:00'),
  'ticker': 'DRC',
  7: 0.069},
 {'date': pd.Timestamp('2020-01-04 00:00:00'),
  'ticker': 'HNG',
  7: 0.023},]
df = pd.DataFrame.from_records(x).set_index('date')
df1 = pd.pivot_table(df, index = df.index, columns = ['ticker'])
df2 = mpd.pivot_table(df, index = df.index, columns = ['ticker'])

Issue Description

When using pivot tables with modin, appear ValueError: can not create pivot table with instance of type <class 'pandas.core.frame.DataFrame'> The regular Pandas works fine. Can anyone help me please?

Expected Behavior

df1 = pd.pivot_table(df, index = df.index, columns = ['ticker'])

Error Logs

```python-traceback Traceback (most recent call last): File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\envs\risk_model\lib\site-packages\IPython\core\interactiveshell.py", line 3398, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 37, in df1 = mpd.pivot_table(df, index = df.index, columns = ['ticker']) File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\envs\risk_model\lib\site-packages\modin\logging\logger_function.py", line 65, in run_and_log return f(*args, **kwargs) File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\envs\risk_model\lib\site-packages\modin\pandas\general.py", line 340, in pivot_table raise ValueError( ValueError: can not create pivot table with instance of type ```

Installed Versions

INSTALLED VERSIONS ------------------ commit : e8093ba372f9adfe79439d90fe74b0b5b6dea9d6 python : 3.8.10.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.22621 machine : AMD64 processor : Intel64 Family 6 Model 167 Stepping 1, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252 pandas : 1.4.3 numpy : 1.23.5 pytz : 2022.7.1 dateutil : 2.8.2 setuptools : 67.6.1 pip : 23.1.2 Cython : 0.29.32 pytest : 7.1.3 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : 3.0.9 lxml.etree : 4.9.1 html5lib : 1.1 pymysql : None psycopg2 : 2.9.3 jinja2 : 3.1.2 IPython : 8.4.0 pandas_datareader: 0.10.0 bs4 : 4.11.1 bottleneck : None brotli : 1.0.9 fastparquet : None fsspec : 2022.8.2 gcsfs : None markupsafe : 2.1.1 matplotlib : 3.5.3 numba : 0.56.2 numexpr : None odfpy : None openpyxl : 3.0.9 pandas_gbq : None pyarrow : 6.0.1 pyreadstat : None pyxlsb : None s3fs : None scipy : 1.9.3 snappy : None sqlalchemy : 1.4.31 tables : None tabulate : 0.8.7 xarray : None xlrd : None xlwt : None zstandard : None
anmyachev commented 1 year ago

@zulee1711 thanks for the contribution!

At the moment, Pandas objects are specifically not accepted by Modin. For this code to work, you need to explicitly convert Pandas' dataframe to Modin's dataframe like that:

mpd.pivot_table(mpd.DataFrame(df), index = df.index, columns = ['ticker'])

@modin-project/modin-core there are more and more situations where it would be convenient for users to pass pandas objects to Modin functions. We should rethink this.

dchigarev commented 1 year ago

there are more and more situations where it would be convenient for users to pass pandas objects to Modin functions. We should rethink this.

I think what we should do is to make a more describable error message in these situations, something like: "you're trying to pass pandas object to a modin's method, please make the conversion as follows...".

It could be a real pain to efficiently support both pandas and modin objects, so my vote here is only to make clear errors.

anmyachev commented 1 year ago

I think what we should do is to make a more describable error message in these situations, something like: "you're trying to pass pandas object to a modin's method, please make the conversion as follows...".

+1

It could be a real pain to efficiently support both pandas and modin objects, so my vote here is only to make clear errors.

We don't have to make the case with pandas dataframes fast (just give a warning that dataframe conversion takes time and if you want performance boost, you need to rewrite the code). Here it is more important to ensure seamless integration with other libraries, where the user does not have the ability to change imports.

zulee1711 commented 1 year ago

@anmyachev Thank you

I think what we should do is to make a more describable error message in these situations, something like: "you're trying to pass pandas object to a modin's method, please make the conversion as follows...".

A more detailed message is much appreciated. Since the error gave a warning in <class 'pandas.core.frame.DataFrame'>, I would know that there's something wrong with the code related to pandas dataframe, but wouldn't necessarily know where to look for an answer

YarShev commented 1 year ago

UPD: Even using Modin objects, I see the following error on latest master.

TypeError: '<' not supported between instances of 'str' and 'Timestamp'