question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: `codes` array of a categorical Series is not writeable and prevents running `__dlpack__()` on `__dataframe__()` outputs

See original GitHub issue

Reproducible Example

>>> import pandas as pd
>>> df = pd.DataFrame({"a_int": [1, 2, 3]})
>>> df["a_cat"] = df["a_int"].astype("category")

>>> df["a_int"].values.flags.writeable
True

>>> df["a_cat"].values.codes.flags.writeable
False

Issue Description

<del>Speaking with @jorisvandenbossche, it seems that this is not intentional and might be caused by the used of the readonly type specialization in the Cython code of the categorical Series.</del> Wrong, see: https://github.com/pandas-dev/pandas/issues/48393#issuecomment-1237057236

The fact that this is not writeable makes it impossible to leverage the __dlpack__() method of the buffers returned by the __dataframe__() interop method because under the hood it would result in calling:

>>> import numpy as np
>>> np.from_dlpack(df.__dataframe__().get_column_by_name("a_cat").get_buffers()['data'][0])
Traceback (most recent call last):
  Input In [6] in <cell line: 1>
    np.from_dlpack(df.__dataframe__().get_column_by_name("a_cat").get_buffers()['data'][0])
  File ~/code/pandas/pandas/core/interchange/buffer.py:57 in __dlpack__
    return self._x.__dlpack__()
TypeError: NumPy currently only supports dlpack for writeable arrays

Expected Behavior

Have the .codes array be writeable, even if the .codes attribute itself is not mutable in the Cython class.

Installed Versions

INSTALLED VERSIONS
------------------
commit           : 298dac384f92726b6da9a6319695cb0269e6eeb8
python           : 3.10.6.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 21.4.0
Version          : Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101
machine          : arm64
processor        : arm
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : None.UTF-8

pandas           : 1.6.0.dev0+49.g298dac384f
numpy            : 1.23.2
pytz             : 2022.2.1
dateutil         : 2.8.2
setuptools       : 65.3.0
pip              : 22.2.2
Cython           : 0.29.32
pytest           : 7.1.2
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 3.1.2
IPython          : 8.4.0
pandas_datareader: None
bs4              : 4.11.1
bottleneck       : None
brotli           : None
fastparquet      : None
fsspec           : None
gcsfs            : None
matplotlib       : 3.5.3
numba            : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : 1.9.1
snappy           : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
zstandard        : None
tzdata           : None

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, Sep 9, 2022

Doesn’t look like merging https://github.com/pandas-dev/pandas/pull/48419 didn’t close this issue for some reason, so doing so manually.

1reaction
jorisvandenbosschecommented, Sep 6, 2022

It’s also only a tiny change, so quickly opened https://github.com/pandas-dev/pandas/pull/48419

Read more comments on GitHub >

github_iconTop Results From Across the Web

Categorical data — pandas 1.5.2 documentation
Internally, the data structure consists of a categories array and an integer array of codes which point to the real value in the...
Read more >
Using pandas categories properly is tricky... here's why
Pandas category errors, categories turn to strings, ... If categorical data can be a pain, then why not just avoid it altogether?
Read more >
Release notes — NumPy v1.24 Manual
Release notes# · isinstance(dtype, np. · Same kind casting in concatenate with axis=None · NumPy Scalars are cast when assigned to arrays ·...
Read more >
NumPy User Guide
TypeError: array() takes from 1 to 2 positional arguments but 4 were given. > ... Note that it is not part of the...
Read more >
tensorflow/RELEASE.md at master · tensorflow/tensorflow · GitHub
Use tensorflow::OkStatus() instead of tensorflow::Status::OK() . Stop constructing Status objects from tensorflow::error::Code . One MUST NOT access ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found