-
-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: DataFrame.pivot
fails with pyarrow
backend if variable is already encoded as dictionary
#53051
Comments
Thanks for the report, looks like we should probably avoid factorizing then in pivot for dictionary pyarrow types. For reference, this is the traceback.
|
take |
For information, same type of issue , with example: _**<class 'pandas.core.frame.DataFrame'> 0 ntusername category test= pd.pivot_table(df, values=['calls_count'], index=[ 'textdata_tables','user','ntusername','date'], aggfunc=np.sum).reset_index()**_ => MemoryError: Unable to allocate 10.8 GiB for an array with shape (8510, 682784) and data type int16 Saving in csv , reloading (pass categories in object) and executing the pivot returns valid pivot in a second. |
Fixed as of #53232 I think. |
With current libraries, (arrow=16.1, pandas=2.2.2, numpy=2.0.0) the example in my OP fails with
|
@lithomas1 The bug seems to be this line: pandas/pandas/core/arrays/categorical.py Line 452 in 39a3bf3
The issue is that values can be one of (ABCIndex, ABCSeries, ExtensionArray), but not all of these have the |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
This bug is caused by apache/arrow#34890: currently, pyarrow's
dictionary_encode
is not idempotent, i.e. it fails withArrowNotImplementedError
instead of returning the data as-is if the array is already of dictionary type.So, either one waits until it is fixed upstream by pyarrow, or an additional check needs to be added to test whether the series is already of dictionary data type.
Expected Behavior
Pivot should work with categorical data when using the pyarrow backend.
Installed Versions
INSTALLED VERSIONS
commit : 37ea63d
python : 3.11.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-41-generic
Version : #42~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 18 17:40:00 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.0.1
numpy : 1.23.5
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : 0.29.34
pytest : 7.3.1
hypothesis : None
sphinx : 7.0.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : 1.0.3
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.13.1
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : 2023.4.0
fsspec : 2023.4.0
gcsfs : None
matplotlib : 3.7.1
numba : 0.57.0rc1
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : 1.4.48
tables : 3.8.0
tabulate : 0.9.0
xarray : 2023.4.2
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: