Skip to content

Commit ec04672

Browse files
committed
PERF: speed up concat on Series by making _get_axis_number() a classmethod
1 parent 2eef865 commit ec04672

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

Diff for: doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ Performance Improvements
10601060
- Improved the performance of :func:`pandas.get_dummies` with ``sparse=True`` (:issue:`21997`)
10611061
- Improved performance of :func:`IndexEngine.get_indexer_non_unique` for sorted, non-unique indexes (:issue:`9466`)
10621062
- Improved performance of :func:`PeriodIndex.unique` (:issue:`23083`)
1063+
- Improved performance of :func:`pd.concat` for `Series` objects (:issue:`23404`)
10631064

10641065

10651066
.. _whatsnew_0240.docs:

Diff for: pandas/core/generic.py

+17-14
Original file line numberDiff line numberDiff line change
@@ -358,41 +358,44 @@ def _from_axes(cls, data, axes, **kwargs):
358358
d.update(kwargs)
359359
return cls(data, **d)
360360

361-
def _get_axis_number(self, axis):
362-
axis = self._AXIS_ALIASES.get(axis, axis)
361+
@classmethod
362+
def _get_axis_number(cls, axis):
363+
axis = cls._AXIS_ALIASES.get(axis, axis)
363364
if is_integer(axis):
364-
if axis in self._AXIS_NAMES:
365+
if axis in cls._AXIS_NAMES:
365366
return axis
366367
else:
367368
try:
368-
return self._AXIS_NUMBERS[axis]
369+
return cls._AXIS_NUMBERS[axis]
369370
except KeyError:
370371
pass
371372
raise ValueError('No axis named {0} for object type {1}'
372-
.format(axis, type(self)))
373+
.format(axis, type(cls)))
373374

374-
def _get_axis_name(self, axis):
375-
axis = self._AXIS_ALIASES.get(axis, axis)
375+
@classmethod
376+
def _get_axis_name(cls, axis):
377+
axis = cls._AXIS_ALIASES.get(axis, axis)
376378
if isinstance(axis, string_types):
377-
if axis in self._AXIS_NUMBERS:
379+
if axis in cls._AXIS_NUMBERS:
378380
return axis
379381
else:
380382
try:
381-
return self._AXIS_NAMES[axis]
383+
return cls._AXIS_NAMES[axis]
382384
except KeyError:
383385
pass
384386
raise ValueError('No axis named {0} for object type {1}'
385-
.format(axis, type(self)))
387+
.format(axis, type(cls)))
386388

387389
def _get_axis(self, axis):
388390
name = self._get_axis_name(axis)
389391
return getattr(self, name)
390392

391-
def _get_block_manager_axis(self, axis):
393+
@classmethod
394+
def _get_block_manager_axis(cls, axis):
392395
"""Map the axis to the block_manager axis."""
393-
axis = self._get_axis_number(axis)
394-
if self._AXIS_REVERSED:
395-
m = self._AXIS_LEN - 1
396+
axis = cls._get_axis_number(axis)
397+
if cls._AXIS_REVERSED:
398+
m = cls._AXIS_LEN - 1
396399
return m - axis
397400
return axis
398401

Diff for: pandas/core/reshape/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
322322

323323
# Standardize axis parameter to int
324324
if isinstance(sample, Series):
325-
axis = DataFrame()._get_axis_number(axis)
325+
axis = DataFrame._get_axis_number(axis)
326326
else:
327327
axis = sample._get_axis_number(axis)
328328

Diff for: pandas/tests/generic/test_generic.py

+12
Original file line numberDiff line numberDiff line change
@@ -1019,3 +1019,15 @@ def test_pipe_panel(self):
10191019

10201020
with pytest.raises(ValueError):
10211021
result = wp.pipe((f, 'y'), x=1, y=1)
1022+
1023+
@pytest.mark.parametrize('box', [pd.Series, pd.DataFrame])
1024+
def test_axis_classmethods(self, box):
1025+
obj = box()
1026+
values = (list(box._AXIS_NAMES.keys()) +
1027+
list(box._AXIS_NUMBERS.keys()) +
1028+
list(box._AXIS_ALIASES.keys()))
1029+
for v in values:
1030+
assert obj._get_axis_number(v) == box._get_axis_number(v)
1031+
assert obj._get_axis_name(v) == box._get_axis_name(v)
1032+
assert obj._get_block_manager_axis(v) == \
1033+
box._get_block_manager_axis(v)

0 commit comments

Comments
 (0)