Skip to content

BUG: Make DataFrame not hardcode it's own constructor in the code. Issue #2859 #2865

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

Closed
Closed
Changes from all commits
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
40 changes: 20 additions & 20 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def axes(self):

@property
def _constructor(self):
return DataFrame
return self.__class__

# Fancy indexing
_ix = None
Expand Down Expand Up @@ -855,15 +855,15 @@ def dot(self, other):
(lvals.shape, rvals.shape))

if isinstance(other, DataFrame):
return DataFrame(np.dot(lvals, rvals),
return self._constructor(np.dot(lvals, rvals),
index=left.index,
columns=other.columns)
elif isinstance(other, Series):
return Series(np.dot(lvals, rvals), index=left.index)
elif isinstance(rvals, np.ndarray):
result = np.dot(lvals, rvals)
if result.ndim == 2:
return DataFrame(result, index=left.index)
return self._constructor(result, index=left.index)
else:
return Series(result, index=left.index)
else: # pragma: no cover
Expand Down Expand Up @@ -902,7 +902,7 @@ def from_dict(cls, data, orient='columns', dtype=None):
elif orient != 'columns': # pragma: no cover
raise ValueError('only recognize index or columns for orient')

return DataFrame(data, index=index, columns=columns, dtype=dtype)
return cls(data, index=index, columns=columns, dtype=dtype)

def to_dict(self, outtype='dict'):
"""
Expand Down Expand Up @@ -969,15 +969,15 @@ def from_records(cls, data, index=None, exclude=None, columns=None,

if com.is_iterator(data):
if nrows == 0:
return DataFrame()
return cls()

try:
if py3compat.PY3:
first_row = next(data)
else:
first_row = data.next()
except StopIteration:
return DataFrame(index=index, columns=columns)
return cls(index=index, columns=columns)

dtype = None
if hasattr(first_row, 'dtype') and first_row.dtype.names:
Expand Down Expand Up @@ -1067,7 +1067,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
mgr = _arrays_to_mgr(arrays, arr_columns, result_index,
columns)

return DataFrame(mgr)
return cls(mgr)

def to_records(self, index=True, convert_datetime64=True):
"""
Expand Down Expand Up @@ -2061,7 +2061,7 @@ def _slice(self, slobj, axis=0):
def _box_item_values(self, key, values):
items = self.columns[self.columns.get_loc(key)]
if values.ndim == 2:
return DataFrame(values.T, columns=items, index=self.index)
return self._constructor(values.T, columns=items, index=self.index)
else:
return Series.from_array(values, index=self.index, name=items)

Expand Down Expand Up @@ -2647,7 +2647,7 @@ def _reindex_multi(self, new_index, new_columns, copy, fill_value):
if row_indexer is not None and col_indexer is not None:
new_values = com.take_2d_multi(self.values, row_indexer,
col_indexer, fill_value=fill_value)
return DataFrame(new_values, index=new_index, columns=new_columns)
return self._constructor(new_values, index=new_index, columns=new_columns)
elif row_indexer is not None:
return self._reindex_with_indexers(new_index, row_indexer,
None, None, copy, fill_value)
Expand Down Expand Up @@ -2695,7 +2695,7 @@ def _reindex_with_indexers(self, index, row_indexer, columns, col_indexer,
if copy and new_data is self._data:
new_data = new_data.copy()

return DataFrame(new_data)
return self._constructor(new_data)

def reindex_like(self, other, method=None, copy=True, limit=None):
"""
Expand Down Expand Up @@ -2938,7 +2938,7 @@ def take(self, indices, axis=0):
if self._is_mixed_type:
if axis == 0:
new_data = self._data.take(indices, axis=1)
return DataFrame(new_data)
return self._constructor(new_data)
else:
new_columns = self.columns.take(indices)
return self.reindex(columns=new_columns)
Expand All @@ -2952,7 +2952,7 @@ def take(self, indices, axis=0):
else:
new_columns = self.columns.take(indices)
new_index = self.index
return DataFrame(new_values, index=new_index,
return self._constructor(new_values, index=new_index,
columns=new_columns)

#----------------------------------------------------------------------
Expand Down Expand Up @@ -4191,7 +4191,7 @@ def _apply_raw(self, func, axis):

# TODO: mixed type case
if result.ndim == 2:
return DataFrame(result, index=self.index,
return self._constructor(result, index=self.index,
columns=self.columns)
else:
return Series(result, index=self._get_agg_axis(axis))
Expand Down Expand Up @@ -4622,7 +4622,7 @@ def describe(self, percentile_width=50):
numdata = self._get_numeric_data()

if len(numdata.columns) == 0:
return DataFrame(dict((k, v.describe())
return self._constructor(dict((k, v.describe())
for k, v in self.iteritems()),
columns=self.columns)

Expand Down Expand Up @@ -5006,7 +5006,7 @@ def _get_agg_axis(self, axis_num):
def _get_numeric_data(self):
if self._is_mixed_type:
num_data = self._data.get_numeric_data()
return DataFrame(num_data, index=self.index, copy=False)
return self._constructor(num_data, index=self.index, copy=False)
else:
if (self.values.dtype != np.object_ and
not issubclass(self.values.dtype.type, np.datetime64)):
Expand All @@ -5017,7 +5017,7 @@ def _get_numeric_data(self):
def _get_bool_data(self):
if self._is_mixed_type:
bool_data = self._data.get_bool_data()
return DataFrame(bool_data, index=self.index, copy=False)
return self._constructor(bool_data, index=self.index, copy=False)
else: # pragma: no cover
if self.values.dtype == np.bool_:
return self
Expand Down Expand Up @@ -5127,7 +5127,7 @@ def rank(self, axis=0, numeric_only=None, method='average',
try:
ranks = algos.rank(self.values, axis=axis, method=method,
ascending=ascending, na_option=na_option)
return DataFrame(ranks, index=self.index, columns=self.columns)
return self._constructor(ranks, index=self.index, columns=self.columns)
except TypeError:
numeric_only = True

Expand All @@ -5137,7 +5137,7 @@ def rank(self, axis=0, numeric_only=None, method='average',
data = self
ranks = algos.rank(data.values, axis=axis, method=method,
ascending=ascending, na_option=na_option)
return DataFrame(ranks, index=data.index, columns=data.columns)
return self._constructor(ranks, index=data.index, columns=data.columns)

def to_timestamp(self, freq=None, how='start', axis=0, copy=True):
"""
Expand Down Expand Up @@ -5170,7 +5170,7 @@ def to_timestamp(self, freq=None, how='start', axis=0, copy=True):
else:
raise ValueError('Axis must be 0 or 1. Got %s' % str(axis))

return DataFrame(new_data)
return self._constructor(new_data)

def to_period(self, freq=None, axis=0, copy=True):
"""
Expand Down Expand Up @@ -5204,7 +5204,7 @@ def to_period(self, freq=None, axis=0, copy=True):
else:
raise ValueError('Axis must be 0 or 1. Got %s' % str(axis))

return DataFrame(new_data)
return self._constructor(new_data)

#----------------------------------------------------------------------
# Deprecated stuff
Expand Down