REF: Add ExtensionArray.map by topper-123 · Pull Request #51809 · pandas-dev/pandas (original) (raw)
- closes ExtensionArray.map #23179
Working with the .map
method is very uncomfortable in main, because of issues with ExtensionArray
:
ExtensionArray
is not required to have a.map
method, but may have it.- Some
ExtensionArray
subclasses, that do have amap
method have one function parameter (mapper
), while other have two parameters (mapper
andna_action
). - There is fallback functionality for calculation
ExtensionArray.map
inIndexOps.Mixin._map_values
. This means we can calculate.map
forExtensionArrays
indirectly, if we wrap them in aSeries
orIndex
, but we cannot rely on the ability to do it directly on theExtensionArray
. ExtensionArray
subclasses often want create their own.map
methods- Because of all the above issues it is often very maze-like to follow code that uses
.map
This PR solves this by:
- moving the functionality in
IndexOpsMixin._map_values
to a functionmap_array
inalgorithms
. - adding a
.map
method toExtensionArray
that calls the newmap_array
function. Subclasses will override this method where needed. - Call the new
map_array
function insideIndexOpsMixin._map_values
. - Ensuring a common signature for the
.map
method forExtensionArray
, i.e. always two method parameters (mapper
andna_action
), so we can reliably call the.map
method on anyExtensionArray
subclass.
Note that this PR doesn't really add new functionality, because we're mostly just moving code around, though it is now possible to call the map
method on any Extensionarray
directly, instead of going through e.g. Series.map
.
In the main branch, there are currently several ExtensionArray
subclasses where na_action='ignore'
doesn't work. I'm working on making it work with all of the ExtensionArray
subclasses and there is already #51645, which handles Categorical
/CategoricalIndex
and more will follow.