Skip to content

Easier sub-classing for Series and DataFrame #4271

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
34 changes: 17 additions & 17 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
return self._combine_series(casted, na_op, fill_value,
axis, level)
elif other.ndim == 2:
casted = DataFrame(other, index=self.index,
casted = self._constructor(other, index=self.index,
columns=self.columns)
return self._combine_frame(casted, na_op, fill_value, level)
else: # pragma: no cover
Expand Down Expand Up @@ -297,7 +297,7 @@ def f(self, other, axis=default_axis, level=None):
return self._combine_series(casted, na_op, None, axis, level)

elif other.ndim == 2:
casted = DataFrame(other, index=self.index,
casted = self._constructor(other, index=self.index,
columns=self.columns)

return self._flex_compare_frame(casted, na_op, str_rep, level)
Expand Down Expand Up @@ -1771,7 +1771,7 @@ def as_blocks(self, columns=None):
bd = dict()
for b in self._data.blocks:
b = b.reindex_items_from(columns or b.items)
bd[str(b.dtype)] = DataFrame(BlockManager([ b ], [ b.items, self.index ]))
bd[str(b.dtype)] = self._constructor(BlockManager([ b ], [ b.items, self.index ]))
return bd

blocks = property(fget=as_blocks)
Expand Down Expand Up @@ -1841,12 +1841,12 @@ def _unpickle_matrix_compat(self, state): # pragma: no cover
(vals, idx, cols), object_state = state

index = _unpickle_array(idx)
dm = DataFrame(vals, index=index, columns=_unpickle_array(cols),
dm = self._constructor(vals, index=index, columns=_unpickle_array(cols),
copy=False)

if object_state is not None:
ovals, _, ocols = object_state
objects = DataFrame(ovals, index=index,
objects = self._constructor(ovals, index=index,
columns=_unpickle_array(ocols),
copy=False)

Expand Down Expand Up @@ -2041,7 +2041,7 @@ def _getitem_multilevel(self, key):
result.columns = result_columns
else:
new_values = self.values[:, loc]
result = DataFrame(new_values, index=self.index,
result = self._constructor(new_values, index=self.index,
columns=result_columns)
if len(result.columns) == 1:
top = result.columns[0]
Expand Down Expand Up @@ -2558,7 +2558,7 @@ def _align_series(self, other, join='outer', axis=None, level=None,
if copy and fdata is self._data:
fdata = fdata.copy()

left_result = DataFrame(fdata)
left_result = self._constructor(fdata)
right_result = other if ridx is None else other.reindex(join_index)

fill_na = notnull(fill_value) or (method is not None)
Expand Down Expand Up @@ -2737,7 +2737,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,
fill_value=NA):
Expand Down Expand Up @@ -2985,7 +2985,7 @@ def take(self, indices, axis=0, convert=True):
if self._is_mixed_type:
if axis == 0:
new_data = self._data.take(indices, axis=1, verify=False)
return DataFrame(new_data)
return self._constructor(new_data)
else:
new_columns = self.columns.take(indices)
return self.reindex(columns=new_columns)
Expand All @@ -2999,7 +2999,7 @@ def take(self, indices, axis=0, convert=True):
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 @@ -4075,7 +4075,7 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
raise NotImplementedError

if not isinstance(other, DataFrame):
other = DataFrame(other)
other = self._constructor(other)

other = other.reindex_like(self)

Expand Down Expand Up @@ -4425,7 +4425,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 @@ -4592,10 +4592,10 @@ def append(self, other, ignore_index=False, verify_integrity=False):

index = None if other.name is None else [other.name]
other = other.reindex(self.columns, copy=False)
other = DataFrame(other.values.reshape((1, len(other))),
other = self._constructor(other.values.reshape((1, len(other))),
index=index, columns=self.columns)
elif isinstance(other, list) and not isinstance(other[0], DataFrame):
other = DataFrame(other)
other = self._constructor(other)
if (self.columns.get_indexer(other.columns) >= 0).all():
other = other.ix[:, self.columns]

Expand Down Expand Up @@ -4660,7 +4660,7 @@ def _join_compat(self, other, on=None, how='left', lsuffix='', rsuffix='',
if isinstance(other, Series):
if other.name is None:
raise AssertionError('Other Series must have a name')
other = DataFrame({other.name: other})
other = self._constructor({other.name: other})

if isinstance(other, DataFrame):
return merge(self, other, left_on=on, how=how,
Expand Down Expand Up @@ -4862,7 +4862,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 @@ -4954,7 +4954,7 @@ def _count_level(self, level, axis=0, numeric_only=False):
labels = com._ensure_int64(frame.index.labels[level])
counts = lib.count_level_2d(mask, labels, len(level_index))

result = DataFrame(counts, index=level_index,
result = self._constructor(counts, index=level_index,
columns=frame.columns)

if axis == 1:
Expand Down
Loading