Skip to content

BUG: ValueError: buffer source array is read-only during groupby #33410

Closed
@erik-hasse

Description

@erik-hasse
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame(data={'x': [1], 'y': [2]})
df.to_parquet('pq_df', partition_cols='x')
df = pd.read_parquet('pq_df')
df.groupby('x', sort=False)

Problem description

The above code raises an exception:

Traceback (most recent call last):
  File "mve.py", line 5, in <module>
    df.groupby('x', sort=False)
  File "/Users/ehasse/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 5798, in groupby
    return groupby_generic.DataFrameGroupBy(
  File "/Users/ehasse/.local/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 402, in __init__
    grouper, exclusions, obj = get_grouper(
  File "/Users/ehasse/.local/lib/python3.8/site-packages/pandas/core/groupby/grouper.py", line 615, in get_grouper
    Grouping(
  File "/Users/ehasse/.local/lib/python3.8/site-packages/pandas/core/groupby/grouper.py", line 312, in __init__
    self.grouper, self.all_grouper = recode_for_groupby(
  File "/Users/ehasse/.local/lib/python3.8/site-packages/pandas/core/groupby/categorical.py", line 72, in recode_for_groupby
    cat = cat.add_categories(c.categories[~c.categories.isin(cat.categories)])
  File "/Users/ehasse/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 4667, in isin
    return algos.isin(self, values)
  File "/Users/ehasse/.local/lib/python3.8/site-packages/pandas/core/algorithms.py", line 447, in isin
    return f(comps, values)
  File "pandas/_libs/hashtable_func_helper.pxi", line 555, in pandas._libs.hashtable.ismember_int64
  File "stringsource", line 658, in View.MemoryView.memoryview_cwrapper
  File "stringsource", line 349, in View.MemoryView.memoryview.__cinit__
ValueError: buffer source array is read-only

This specifically requires the following:

  • The dataframe is loaded from a parquet file.
  • The column being grouped by was used to partition the file.
  • sort=False is passed.

In addtion, passing observed=True stops the error from occurring.

I believe this is related to #31710, but they were unable to provide an example for groupby, and the issue remains on 1.0.3.

Expected Output

A DataFrameGroupBy object.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.0.0.post20200309
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.47.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions