You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Categorical can currently be converted to a numpy array by using the to_numpy method. This is fine if the underlying array is a numpy array, but if it is an ExtensionArray it is is not possible to get an ExtensionArray from a Categorical, except recreating the ExtensionArray manually.
For example for StringArray:
>>>importpandasaspd>>>arr=pd.array(["a", pd.NA])
>>>cat=pd.Categorical(arr)
>>>cat.to_numpy()
array(['a', <NA>], dtype=object) # does not maintain dtype>>>cat.categories.array[cat.codes] # manual method for getting the desired array type and dtype<StringArray>
['a', 'a']
Length: 2, dtype: string
Feature Description
I propose adding a to_array method to Categorical, CategoricalAccessor & CategoricalIndex, which will return an array of the appropriate type (numpy array or ExtensionArray) of the same length as the Categorical. It probably should be possible to convert to and from Categoricals and ExtensionArrays/numpy.ndarrays losslessly and this should be tested for.
Alternative Solutions
The alternative would be to create the underlying array manually, as in the example above.
Additional Context
No response
The text was updated successfully, but these errors were encountered:
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
A
Categorical
can currently be converted to a numpy array by using theto_numpy
method. This is fine if the underlying array is a numpy array, but if it is anExtensionArray
it is is not possible to get anExtensionArray
from aCategorical
, except recreating theExtensionArray
manually.For example for
StringArray
:Feature Description
I propose adding a
to_array
method toCategorical
,CategoricalAccessor
&CategoricalIndex
, which will return an array of the appropriate type (numpy array or ExtensionArray) of the same length as the Categorical. It probably should be possible to convert to and fromCategorical
s andExtensionArrays
/numpy.ndarray
s losslessly and this should be tested for.Alternative Solutions
The alternative would be to create the underlying array manually, as in the example above.
Additional Context
No response
The text was updated successfully, but these errors were encountered: