Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF: de-privatize dtypes.concat functions #27499

Merged
merged 6 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move private functions to the one place they are used
  • Loading branch information
jbrockmendel committed Jul 20, 2019
commit cad178c32d465b84e5846278b774ac8a0570e4d9
35 changes: 0 additions & 35 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
ABCIndexClass,
ABCPeriodIndex,
ABCRangeIndex,
ABCSparseDataFrame,
ABCTimedeltaIndex,
)

Expand Down Expand Up @@ -71,40 +70,6 @@ def get_dtype_kinds(l):
return typs


def _get_series_result_type(result, objs=None):
"""
return appropriate class of Series concat
input is either dict or array-like
"""
from pandas import SparseSeries, SparseDataFrame, DataFrame

# concat Series with axis 1
if isinstance(result, dict):
# concat Series with axis 1
if all(isinstance(c, (SparseSeries, SparseDataFrame)) for c in result.values()):
return SparseDataFrame
else:
return DataFrame

# otherwise it is a SingleBlockManager (axis = 0)
return objs[0]._constructor


def _get_frame_result_type(result, objs):
"""
return appropriate class of DataFrame-like concat
if all blocks are sparse, return SparseDataFrame
otherwise, return 1st obj
"""

if result.blocks and (any(isinstance(obj, ABCSparseDataFrame) for obj in objs)):
from pandas.core.sparse.api import SparseDataFrame

return SparseDataFrame
else:
return next(obj for obj in objs if not isinstance(obj, ABCSparseDataFrame))


def concat_compat(to_concat, axis=0):
"""
provide concatenation of an array of arrays each of which is a single
Expand Down
42 changes: 38 additions & 4 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np

import pandas.core.dtypes.concat as _concat
from pandas.core.dtypes.generic import ABCSparseDataFrame

from pandas import DataFrame, Index, MultiIndex, Series
from pandas.core import common as com
Expand Down Expand Up @@ -439,13 +439,13 @@ def get_result(self):
mgr = self.objs[0]._data.concat(
[x._data for x in self.objs], self.new_axes
)
cons = _concat._get_series_result_type(mgr, self.objs)
cons = _get_series_result_type(mgr, self.objs)
return cons(mgr, name=name).__finalize__(self, method="concat")

# combine as columns in a frame
else:
data = dict(zip(range(len(self.objs)), self.objs))
cons = _concat._get_series_result_type(data)
cons = _get_series_result_type(data)

index, columns = self.new_axes
df = cons(data, index=index)
Expand Down Expand Up @@ -475,7 +475,7 @@ def get_result(self):
if not self.copy:
new_data._consolidate_inplace()

cons = _concat._get_frame_result_type(new_data, self.objs)
cons = _get_frame_result_type(new_data, self.objs)
return cons._from_axes(new_data, self.new_axes).__finalize__(
self, method="concat"
)
Expand Down Expand Up @@ -708,3 +708,37 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None):
return MultiIndex(
levels=new_levels, codes=new_codes, names=new_names, verify_integrity=False
)


def _get_series_result_type(result, objs=None):
"""
return appropriate class of Series concat
input is either dict or array-like
"""
from pandas import SparseSeries, SparseDataFrame, DataFrame

# concat Series with axis 1
if isinstance(result, dict):
# concat Series with axis 1
if all(isinstance(c, (SparseSeries, SparseDataFrame)) for c in result.values()):
return SparseDataFrame
else:
return DataFrame

# otherwise it is a SingleBlockManager (axis = 0)
return objs[0]._constructor


def _get_frame_result_type(result, objs):
"""
return appropriate class of DataFrame-like concat
if all blocks are sparse, return SparseDataFrame
otherwise, return 1st obj
"""

if result.blocks and (any(isinstance(obj, ABCSparseDataFrame) for obj in objs)):
from pandas.core.sparse.api import SparseDataFrame

return SparseDataFrame
else:
return next(obj for obj in objs if not isinstance(obj, ABCSparseDataFrame))