From 330b8c1c195174f729a1d2ee6f916ebd1579217e Mon Sep 17 00:00:00 2001 From: Pietro Battiston Date: Sat, 12 Aug 2017 19:33:52 +0200 Subject: [PATCH] REF: _get_objs_combined_axis (#17217) --- pandas/core/frame.py | 6 ++---- pandas/core/indexes/api.py | 11 +++++++++++ pandas/core/panel.py | 7 +++---- pandas/core/reshape/concat.py | 19 ++++++++----------- pandas/core/reshape/pivot.py | 9 ++------- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2c82fe4c348d5..467ef52de234e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6126,12 +6126,10 @@ def _list_to_arrays(data, columns, coerce_float=False, dtype=None): def _list_of_series_to_arrays(data, columns, coerce_float=False, dtype=None): - from pandas.core.index import _get_combined_index + from pandas.core.index import _get_objs_combined_axis if columns is None: - columns = _get_combined_index([ - s.index for s in data if getattr(s, 'index', None) is not None - ]) + columns = _get_objs_combined_axis(data) indexer_cache = {} diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index d90c681abc03f..db73a6878258a 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -23,11 +23,22 @@ 'PeriodIndex', 'DatetimeIndex', '_new_Index', 'NaT', '_ensure_index', '_get_na_value', '_get_combined_index', + '_get_objs_combined_axis', '_get_distinct_indexes', '_union_indexes', '_get_consensus_names', '_all_indexes_same'] +def _get_objs_combined_axis(objs, intersect=False, axis=0): + # Extract combined index: return intersection or union (depending on the + # value of "intersect") of indexes on given axis, or None if all objects + # lack indexes (e.g. they are numpy arrays) + obs_idxes = [obj._get_axis(axis) for obj in objs + if hasattr(obj, '_get_axis')] + if obs_idxes: + return _get_combined_index(obs_idxes, intersect=intersect) + + def _get_combined_index(indexes, intersect=False): # TODO: handle index names! indexes = _get_distinct_indexes(indexes) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index e4515efe109c5..a3e35492ad9af 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -26,7 +26,7 @@ from pandas.core.frame import DataFrame from pandas.core.generic import NDFrame, _shared_docs from pandas.core.index import (Index, MultiIndex, _ensure_index, - _get_combined_index) + _get_objs_combined_axis) from pandas.io.formats.printing import pprint_thing from pandas.core.indexing import maybe_droplevels from pandas.core.internals import (BlockManager, @@ -1448,7 +1448,6 @@ def _extract_axis(self, data, axis=0, intersect=False): index = Index([]) elif len(data) > 0: raw_lengths = [] - indexes = [] have_raw_arrays = False have_frames = False @@ -1456,13 +1455,13 @@ def _extract_axis(self, data, axis=0, intersect=False): for v in data.values(): if isinstance(v, self._constructor_sliced): have_frames = True - indexes.append(v._get_axis(axis)) elif v is not None: have_raw_arrays = True raw_lengths.append(v.shape[axis]) if have_frames: - index = _get_combined_index(indexes, intersect=intersect) + index = _get_objs_combined_axis(data.values(), axis=axis, + intersect=intersect) if have_raw_arrays: lengths = list(set(raw_lengths)) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index e199ec2710367..20d561738dc78 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -4,7 +4,7 @@ import numpy as np from pandas import compat, DataFrame, Series, Index, MultiIndex -from pandas.core.index import (_get_combined_index, +from pandas.core.index import (_get_objs_combined_axis, _ensure_index, _get_consensus_names, _all_indexes_same) from pandas.core.categorical import (_factorize_from_iterable, @@ -445,16 +445,13 @@ def _get_new_axes(self): return new_axes def _get_comb_axis(self, i): - if self._is_series: - all_indexes = [x.index for x in self.objs] - else: - try: - all_indexes = [x._data.axes[i] for x in self.objs] - except IndexError: - types = [type(x).__name__ for x in self.objs] - raise TypeError("Cannot concatenate list of %s" % types) - - return _get_combined_index(all_indexes, intersect=self.intersect) + data_axis = self.objs[0]._get_block_manager_axis(i) + try: + return _get_objs_combined_axis(self.objs, axis=data_axis, + intersect=self.intersect) + except IndexError: + types = [type(x).__name__ for x in self.objs] + raise TypeError("Cannot concatenate list of %s" % types) def _get_concat_axis(self): """ diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index e61adf3aac30a..e08c307bba818 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -8,7 +8,7 @@ from pandas.core.series import Series from pandas.core.groupby import Grouper from pandas.core.reshape.util import cartesian_product -from pandas.core.index import Index, _get_combined_index +from pandas.core.index import Index, _get_objs_combined_axis from pandas.compat import range, lrange, zip from pandas import compat import pandas.core.common as com @@ -440,12 +440,7 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None, rownames = _get_names(index, rownames, prefix='row') colnames = _get_names(columns, colnames, prefix='col') - obs_idxes = [obj.index for objs in (index, columns) for obj in objs - if hasattr(obj, 'index')] - if obs_idxes: - common_idx = _get_combined_index(obs_idxes, intersect=True) - else: - common_idx = None + common_idx = _get_objs_combined_axis(index + columns, intersect=True) data = {} data.update(zip(rownames, index))